如何从包含可点击项目 (imagebuttons) 的 ListView 中获取行项目的 ID(每行有 3 个项目)?

How do I get the ID of the item of row (each row has 3 items) from a ListView, which contains a clickable items (imagebuttons)?

我在Whosebug上看了很多关于这个问题的文章,但都没有解决我的问题。我有包含行的 ListView,每行有 3 个图像按钮,当我单击 "setOnItemClickListener" 中的图像按钮代码时不起作用。

我需要点击行中的哪个项目(图像按钮)。在我的 ListView 中会有很多行。

我听说过 simpleCursorAdapter、fragmentlist、cursorloader,但是我需要的最低 SDK 是 10

在这段代码中,我使用了 6 张图片,但我打算使用更多图片 (100)。

我的java代码:

package foxstrot.ghp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class images extends  Activity {

    int[] images = {R.raw.a2, R.raw.a3, R.raw.im2, R.raw.a4, R.raw.a5, R.raw.im6,};

    ListView lv;

    Intent i = new Intent(this, king.class);

    final String LOG_TAG = "L";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images);



        lv = (ListView) findViewById(R.id.lv);

        ArrayList<Map <String, Object>> data = new ArrayList<Map<String, Object>>(images.length);
        Map<String, Object> m;

        for(int i = 0; i < images.length; i++){


            m = new HashMap<String, Object>();
            m.put("image1", images[i]);
            m.put("image2", images[i+1]);
            m.put("image3", images[i+2]);
            i = i + 2;
            data.add(m);

        }

        String[] from = {"image1","image2","image3"};
        int[] to = {R.id.ib1, R.id.ib2, R.id.ib3};

        SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.a_, from, to);

        lv.setAdapter(sAdapter);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                i.putExtra("id", id);
                startActivity(i);
                Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
                        + id);
            }
        });






    }












}

我的 XML 代码(在适配器中使用的容器)

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



        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ib1"
            android:layout_weight="1"
            android:scaleType="fitXY"

            />

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ib2"
            android:layout_weight="1"
            android:scaleType="fitXY"
            />

        <ImageButton
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ib3"
            android:layout_weight="1"
            android:scaleType="fitXY"
            />



</LinearLayout>

在您的 images.xml 布局文件中,将 ImageButtonsclickablefocusable 属性设置为 false。按钮上的点击现在将传递并由 ListView.

处理
<ImageButton
    android:id="@+id/ib1"
    ...
    android:clickable="false"
    android:focusable="false" />

setOnItemClickListener() 用于检测对列表项(行)的点击,而不是对这些项(行)上的特定按钮的点击。

对于 xml 行中的每个按钮,您需要添加一个 onClick 属性,该属性具有处理点击的功能:android:onClick="myFunction".

在您的 activity 上,您必须创建一个函数:

public void myFunction(View v) {...}

如果您需要传递有关单击哪一行的特定信息,您可以在适配器的 getView() 方法的按钮视图上添加一个标签。有关此检查的更多信息:How do I get the row ID of the row from a ListView, which contains a clickable item?