簡體   English   中英

如何創建具有多個視圖的列表行項目

[英]how to create a list row item with more than one view

我有一個列表項有多個視圖,一個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;
  }

您可以在本教程中找到滿足您需求的有效示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM