Android 使用自定义适配器从 ListView 中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10539996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:06:27  来源:igfitidea点击:

Remove items from ListView with a custom adapter

androidandroid-listviewcustomizationadapter

提问by Merve

I have a custom ListViewand 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 :

我有一个自定义ListView和适配器。我可以从我的自定义列表中设置的列表中删除一个项目,但我可以从ListView. 当我尝试调用adapter.remove(position)编辑器时说“创建一个方法"remove(int position)""。我不知道当我在适配器中创建这个方法时我该怎么做。代码:

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:

当我点击“delete_Button”时:我只能从列表中删除:

konulist.remove(konulist.get(position));;
kimdenlist.remove(kimdenlist.get(position));

回答by vandzi

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

这是因为您的 listViewAdapter 没有删除方法!您扩展了 BaseAdapter 并且它没有 remove 方法。你应该在 listviewAdapter 中创建 remove 方法,它看起来像

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...

您必须了解列表视图和适配器的工作原理。适配器保存列表视图的数据。当要创建列表行时调用适配器方法 getView。列表大小由适配器的 getCount() 等返回的值计算...

回答by flagg327

To remove an item from ListView BUT NOT INSIDE ADAPTER CLASS:

要从 ListView 但不在适配器类中删除项目:

lv.removeViewAt(index);
adapter.notifyDataSetChanged();

where "index" specifies the position or index in the ListView that holds the item to delete.

其中“index”指定 ListView 中保存要删除的项目的位置或索引。

To remove an item from ListView INSIDE ADAPTER CLASS: First you need to add a Tag to each item in the list. use some layout within the content item in the list to assign that Tag. This can be done within the method getView ().

从 ListView INSIDE ADAPTER CLASS 中删除项目: 首先,您需要为列表中的每个项目添加一个标签。在列表中的内容项中使用一些布局来分配该标签。这可以在方法 getView() 内完成。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    row = convertView;

    if(row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.item_lista_lugares_visitar, parent, false);
        holder = new ViewHolder();

        // ... DO WHAT YOU NEED HERE
        holder.linearLayoutContainer = (LinearLayout) row.findViewById(R.id.ll_container);
        // Set the position as a Tag for the view
        holder.linearLayoutContainer.setTag(position);

    } else {
        holder = (ViewHolder) row.getTag();
    }

    // ... DO WHAT YOU NEED HERE

    return row;
}

// Method for remove an item of ListView inside adapter class
// you need to pass as an argument the tag you added to the layout of your choice
public void removeView(Object position) {
      // lv and the adapter must be public-static in their Activity Class
      SomeActivity.lv.removeViewAt(Integer.parteInt(position).toString());
      SomeActivity.adapter.notifyDataSetChanged();
}

回答by Felipe Oliveira

After your code

在你的代码之后

konulist.remove(konulist.get(position)); kimdenlist.remove(kimdenlist.get(position));

konulist.remove(konulist.get(position)); kimdenlist.remove(kimdenlist.get(position));

you can call the method:

您可以调用该方法:

notifyDataSetChanged();

notifyDataSetChanged();