如何创建具有多个视图的列表行项目

时间:2014-01-21 09:15:32

标签: java android arrays android-arrayadapter

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

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:请提供代码

1 个答案:

答案 0 :(得分:1)

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

并在此自定义适配器(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中找到有关您的要求的工作示例。