Android 在自定义适配器中单击以删除列表项

Android 在自定义适配器中单击以删除列表项,android,android-arrayadapter,android-adapter,baseadapter,listadapter,Android,Android Arrayadapter,Android Adapter,Baseadapter,Listadapter,我有一个自定义的ArrayAdapter,其中我使用了两种不同的布局 列表项1具有不同的布局,其他列表项具有相同的布局 列表项1有一个包含按钮和文本视图的布局。我希望当有人单击列表中的按钮时,列表项1被删除 class MyAdapter extends ArrayAdapter<Product> { @Override public int getViewTypeCount() { return 2; } @Override public int getItemView

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

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

列表项1有一个包含按钮和文本视图的布局。我希望当有人单击列表中的按钮时,列表项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;
}

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


如何在MyAdapter中传递数据?MyAdapter=新MyAdapter此,placeProducts.products;listView.setAdapteradapter;产品是ArrayList还是任何其他数据结构?它是一个列表。
    public void onClick(View v) {
      // delete this first list item here
       products.remove(0);
       notifyDataSetChanged();
    }