【问题标题】:Access To items in a Listview in Android访问 Android 列表视图中的项目
【发布时间】:2016-03-28 19:40:19
【问题描述】:

我有一个名为 list_item.xml 的文件 XML,其中包含以下代码:

<LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="20dp"
      android:layout_weight="2"
      android:orientation="horizontal">


      <ImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:id="@+id/telephone"
            android:src="@drawable/telephone"/>

      <ImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:id="@+id/favorie"
            android:src="@drawable/heart"/>

      <ImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/consulter"
            android:src="@drawable/consulter"/>

</LinearLayout>   

在 java 代码中我有活动:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultat_recherche);

xml文件的代码(resultat_recherche.xml):

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

我在这个列表中填写了一个适配器:

ListAdapter adapter = new SimpleAdapter(
            Resultat_recherche.this, contactList,
     R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});

setListAdapter(adapter);

现在我想从 list_item (ImageView) 中恢复元素来管理点击和启动意图。

我想例如当我点击 imageview(telephone) 时我会做一些事情

我怎么能在这个活动中做到这一点? 谢谢

【问题讨论】:

标签: android listview android-imageview android-adapter


【解决方案1】:

首先在您的 list_item.xml 中为每个 ImageView 添加 id 并假设 img1、img2、img3 依次扩展 SimpleAdapter 并覆盖 getView,如下所示

 class CusAdapter extends android.widget.SimpleAdapter {

public CusAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view =  super.getView(position,convertView,parent);
    ImageView img1 = (ImageView)view.findViewById(R.id.img);
    img1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //do some thing
        }
    });
    ImageView img2 = (ImageView)view.findViewById(R.id.img);
    img2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do some thing
        }
    });
    ImageView img3 = (ImageView)view.findViewById(R.id.img);
    img3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do some thing
        }
    });
    return view;
}

}

然后使用 CusAdapter

ListAdapter adapter = new CusAdapter(
        Resultat_recherche.this, contactList,
 R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});

setListAdapter(adapter);

【讨论】:

    【解决方案2】:

    我认为您正在寻找 ListView 的 OnItemClickListener。

    像这样实现它

    listviewobject.setOnItemClickListener(new OnItemClickListener()
       {
       @Override
       public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
       {
            String value = (String)adapter.getItemAtPosition(position); 
            // assuming string and if you want to get the value on click of list item
            // do what you intend to do on click of listview row
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多