Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove items from ListView with a custom adapter

I have a custom ListView and adapter. I can delete an item from my lists which are set on my custom list, but I can delete from ListView. When I try to call adapter.remove(position) the editor is saying to "create a method "remove(int position)"". I don't know what should I do when I create this method into the adapter. Code :

Filling my listview:

lv = (ListView) findViewById(R.id.list);
        LayoutInflater mLInflater = getLayoutInflater();
        final ListViewAdapter adapter = new ListViewAdapter(
                getApplicationContext(), kimdenlist, konulist,
                mLInflater);
        lv.setAdapter(adapter);

ListViewAdapter:

public class ListViewAdapter extends BaseAdapter {
    static HashMap<Integer, Boolean> cartItems = new HashMap<Integer, Boolean>();
    Context mContext;
    ArrayList<String> kimdenlist; // to load images
    ArrayList<String> konulist; // for data
    LayoutInflater mLayoutInflater;

    public ListViewAdapter(Context context, ArrayList<String> kimdenlist, ArrayList<String> konulist,
            LayoutInflater layoutInflater) {
        mContext = context;
        this.kimdenlist = kimdenlist;
        this.konulist = konulist;
        mLayoutInflater = layoutInflater;
    }

    @Override
    public int getCount() 
    {

        return kimdenlist.size(); // images array length
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    int count = 0;

    // customized Listview
    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {

        View v;
        final int pos = position;
        v = mLayoutInflater.inflate(R.layout.listust, null);

        TextView kimden = (TextView) v.findViewById(R.id.textvKimden);
        kimden.setText(kimdenlist.get(position));
        TextView konu = (TextView) v.findViewById(R.id.textvKonu);
        konu.setText(konulist.get(position));
        CheckBox ch = (CheckBox) v.findViewById(R.id.chk);
        try {
            if (count != 0) {
                boolean b = cartItems.get(pos);
                if (b == false)
                    ch.setChecked(false);
                else
                    ch.setChecked(true);
            }
        } catch (NullPointerException e) {

        }


        ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                cartItems.put(pos, arg1);
                count++;

            }
        });
        return v;
    }

    public static HashMap<Integer, Boolean> getcartItems() {
        return cartItems;
    }

}

When I click to "delete_Button" : I can only remove from lists:

konulist.remove(konulist.get(position));;
kimdenlist.remove(kimdenlist.get(position));
like image 264
Merve Avatar asked Oct 17 '25 19:10

Merve


1 Answers

It's because your listViewAdapter has not remove method! You extend BaseAdapter and it has not remove method. You shoud create remove method in listviewAdapter and it will looks like

public void remove(int position){
    konulist.remove(konulist.get(position));;
    kimdenlist.remove(kimdenlist.get(position));
}

You have to understand how list view and adapter works. Adapter holds data for listview. Adapter method getView is called when list line is going to be created. List size is calculated by value returned by adapter's getCount() and so on...

like image 172
Martin Vandzura Avatar answered Oct 19 '25 09:10

Martin Vandzura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!