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

android android-listview customization adapter

17665 观看

3回复

714 作者的声誉

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

填写我的列表视图:

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

}

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

konulist.remove(konulist.get(position));;
kimdenlist.remove(kimdenlist.get(position));
作者: Merve 的来源 发布者: 2012 年 5 月 10 日

回应 3


11

1677 作者的声誉

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

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

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

作者: vandzi 发布者: 2012 年 5 月 10 日

1

479 作者的声誉

从ListView中移除项目但不包括内部适配器类:

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

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

要从ListView INSIDE ADAPTER CLASS中删除项目:首先,您需要为列表中的每个项目添加一个Tag。在列表中的内容项中使用一些布局来分配该标记。这可以在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();
}
作者: flagg327 发布者: 2015 年 4 月 13 日

0

26 作者的声誉

你的代码之后

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

你可以调用方法:

notifyDataSetChanged();

作者: Felipe Oliveira 发布者: 2018 年 11 月 23 日
32x32