【问题标题】:Remove a list-item on click inside a custom adapter在自定义适配器内单击时删除列表项
【发布时间】:2015-01-09 09:28:07
【问题描述】:

我有一个自定义的 ArrayAdapter,我在其中使用了两种不同的布局。

列表项 1 具有不同的布局,而其他列表项具有相同的布局。

列表项一具有包括按钮和文本视图的布局。当有人单击其中的按钮时,我希望删除列表项之一。

class MyAdapter extends ArrayAdapter<Product> {

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return VIEW_TYPE0;
    }
    return VIEW_TYPE1;
}

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

    final Product product = getItem(position);

    if (convertView == null) {
        if (getItemViewType(position) == VIEW_TYPE0) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout1, parent, false);
        } else {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout2, parent, false);
        }
    }

    TextView textview1;
    TextView textview2;
    Button button1;

    if (position == 0) {
        button1 = (Button) convertView.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              // delete this first list item here
            }
        });
    } else {
        textview = (TextView) convertView.findViewById(R.id.textview1);
        textview.setText(product.product_name);
    }
    return convertView;
}

【问题讨论】:

  • 您是如何在MyAdapter 中传递数据的?
  • MyAdapter 适配器 = new MyAdapter(this, placeProducts.products); listView.setAdapter(适配器);
  • products 是 ArrayList 还是其他任何数据结构?
  • 它是一个列表

标签: android android-arrayadapter android-adapter baseadapter listadapter


【解决方案1】:

尝试使用 List 的 remove 方法并调用 notifyDataSetChanged

    public void onClick(View v) {
      // delete this first list item here
       products.remove(0);
       notifyDataSetChanged();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多