N
N
nikita_chiru2016-08-03 16:28:54
Java
nikita_chiru, 2016-08-03 16:28:54

How to update a list after deleting a row in a database using Sugar orm?

Help, who knows
, it turns out that the list is filled from the database, but if I delete a line in the database, then nothing changes in the list until you go to another activity and back (that is, you pressed the button, a change occurred in the database, but the list remained just like it was, but if I start an activity, the change happens) ... how can I change the list dynamically?

public class Korzina extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    private SwipeLayoutAdapterKorz mAdapter;
    private ListView mListView;
    Korz mData= new Korz();
    ImageButton button,button2,button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_korzina);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        button = (ImageButton) findViewById(R.id.button);
        button2 = (ImageButton) findViewById(R.id.button2);
        button3 = (ImageButton) findViewById(R.id.button3);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        List<Korz> mData = Korz.listAll(Korz.class);//это список , который берётся из бд 
        mListView = (ListView)findViewById(R.id.listView);
        mAdapter = new MyAdapater(this,R.layout.item_korz,R.layout.item_action,mData);
        mListView.setAdapter(mAdapter);




    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }


   

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

 

    class MyAdapater extends SwipeLayoutAdapterKorz<Korz>
    {
        private List<Korz> _data;

        ArrayList objects;
        public MyAdapater(Activity context, int contentViewResourceId, int actionViewResourceId, List<Korz> objects)
        {
            super(context,contentViewResourceId,actionViewResourceId,objects);
            _data = objects;

        }

        
        @Override
        public void setContentView(View contentView, int position, HorizontalScrollView parent) {
            Korz p=((Korz) getItem(position));
            TextView pricecent = (TextView)contentView.findViewById(R.id.pricecent);
            pricecent.setText(p.price+"руб");
            TextView tv1 = (TextView)contentView.findViewById(R.id.name);
            tv1.setText(p.name);
            ImageView tv3 = (ImageView) contentView.findViewById(R.id.imageView2);
            int resID = getResources().getIdentifier(String.valueOf(p.image), "drawable", getApplicationContext().getPackageName());
            tv3.setImageResource(resID);
        }

        //实现setActionView方法
        @Override
        public void setActionView(final View contentView,final   int position, final HorizontalScrollView parent) {

            Korz p=((Korz) getItem(position));
            final String name = p.name;

            contentView.findViewById(R.id.action).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Korz.deleteAll(Korz.class, "name = ?", name);//по нажатию этой кнопки строка в бд удаляется
                   




                }
            });
           

        }


    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nikita_chiru, 2016-08-03
@nikita_chiru

in my case the solution is

public void onClick(View v) {
                    Korz.deleteAll(Korz.class, "name = ?", name);
                    _data.remove(position);

                    mAdapter.notifyDataSetChanged();

                }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question