【问题标题】:how to create a list row item with more than one view如何创建具有多个视图的列表行项
【发布时间】:2014-01-21 09:15:31
【问题描述】:

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

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;
  }

我该如何解决这个问题,以便我能够删除行项目并更新数组适配器? ps:也请提供代码

【问题讨论】:

  • 使用 BaseAdapter 代替 ArrayAdapter。
  • UMM 您可以使用ArrayListremove() 函数从list 中删除数据,然后使用notifyDataSetChanged();

标签: java android arrays android-arrayadapter


【解决方案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 中找到您的要求的工作示例。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多