我有一个列表项有多个视图,一个textview和一个imagebutton,但是ArrayAdapter的结构只允许一个TextView,因此我无法查看并单击imagebutton来删除行项目,我的代码是如下:

MainActivity.java

ArrayList<String> arrayvalues= new ArrayList<String>();
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv=(ListView) findViewById(R.id.list);


adapter = new ArrayAdapter<String>(this,
              R.layout.list_item, R.id.tv_num, arrayvalues);
lv.setAdapter(adapter);


这是我的列表行模板list_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
     <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal"
   >

 <TextView
  android:id="@+id/tv_num"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="left"
  android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
    android:id="@+id/btn_delete"
    android:layout_width="wrap_content"
    android:layout_height="50dip"
    android:src="@drawable/ic_launcher"/>

 </LinearLayout>

</LinearLayout>


单击ImageButton应该删除行项,如下所示

  public View getView(final int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
     LayoutInflater mInflater = null;
     final ImageButton btn_delete;

      View vi=convertView;
      if(convertView==null)
       vi = mInflater.inflate(R.layout.list_item, null);
      btn_delete=(ImageButton) findViewById(R.id.btn_delete);
      btn_delete.setTag(position);
      btn_delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

              adapter.remove(adapter.getItem(position));
              adapter.notifyDataSetChanged();


        }
    });


      return vi;
  }


如何解决此问题,以便能够删除行项目并更新arrayadapter?
ps:也请提供代码

最佳答案

您需要创建自己的自定义适配器。

在此自定义适配器(MySimpleArrayAdapter)中,膨胀自定义子布局。请参阅以下示例以了解自定义适配器。

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_item, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageButton imageBtn = (ImageButton) rowView.findViewById(R.id.btn_delete);
    textView.setText(values[position]);
    // TODO get the id of image button here and delete


    return rowView;
  }


您可以在此tutorial中找到满足您需求的有效示例。

关于java - 如何创建具有多个 View 的列表行项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21253716/

10-10 10:02