N
N
nikita_chiru2016-08-02 15:27:24
Java
nikita_chiru, 2016-08-02 15:27:24

How to remove row from listView on button click?

how to delete lines from the list on button click?

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);


        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);




    }


    private ArrayList<Long> getDataSet() {
        List<Korz> books = Korz.listAll(Korz.class);
        ArrayList<Long> mDataSet = new ArrayList<Long>();

       
    }

    @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;
    }


    @SuppressWarnings("StatementWithEmptyBody")
    
    
        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);//здесь удаляется из бд , а мне бы хотелось , что б одновременно удалялось ещё и из списка 




                }
            });
            contentView.findViewById(R.id.star).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {







                }
            });

        }


    }
}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Vishnevsky, 2016-08-11
@Tema_man

If you do not change this code, then you can do this:

class MyAdapater extends SwipeLayoutAdapterKorz<Korz>
{

   //......

    @Override
    public void setActionView(final View contentView,final   int position, final HorizontalScrollView parent) {
    
        final Korz p=((Korz) getItem(position));
        final String name = p.name;

        contentView.findViewById(R.id.action).setOnClickListener(new View.OnClickListener() {
            @Overrided 
            public void onClick(View v) {
               //здесь удаляется из бд...
               Korz.deleteAll(Korz.class, "name = ?", name);

               // Удаляем элемент из списка в адаптере
               _data.remove(p);

              // Уведомляем адаптер, что данные изменились и надо перерисовать View
               notifyDataSetChanged();
            }
        });
     
 // .....
}

But I would strongly recommend that you refactor and switch to a more modern Recycler View

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question