java 将 onClickListener 添加到列表视图项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6867372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:38:56  来源:igfitidea点击:

Add onClickListener to listview item

javaandroidlistview

提问by Denoteone

I have a list view of a thumbnail and text next to each other. I am trying to figure out how to add a onClicklistner to each list item so when the user selects the text or thumbnails the full image will pop up.Below is my list object and adapter and the lazyAdapter code.

我有一个缩略图和文本的列表视图。我想弄清楚如何将 onClicklistner 添加到每个列表项,以便当用户选择文本或缩略图时,完整图像将弹出。下面是我的列表对象和适配器以及 lazyAdapter 代码。

MainActivity:

主要活动:

list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings, mImages);
list.setAdapter(adapter);

LazyAdapter:

懒惰适配器:

  public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

    TextView text=(TextView)vi.findViewById(R.id.text);;
    ImageView image=(ImageView)vi.findViewById(R.id.image);
    text.setText(image_name[position]);
    imageLoader.DisplayImage(data[position], activity, image);
    return vi;
}

EDIT THis is what I ended up using.

编辑这是我最终使用的。

                list.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
                        Toast.makeText(MainActivity.this, "Show Full Image", Toast.LENGTH_LONG).show();
                    }
                });

回答by user802421

You can use mStringsand mImagesin your OnItemClickListener. Assumed from your LazyAdapter that they are arrays. Maybe you can try something like this.

您可以使用mStringsmImages您的OnItemClickListener。从你的 LazyAdapter 假设它们是数组。也许你可以尝试这样的事情。

list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        String text = mStrings[position];
        YourImageClass img = mImages[position];
        Intent i = new Intent(MainActivity.this, ShowFullImageActivity.class);
        i.putExtra("TEXT", text);
        i.putExtra("IMAGE", img); // <-- Assumed you image is Parcelable
        startActivity(i);
    }
}

回答by Ken

What may help you is implementing an AdapterView.OnItemClickListener.

实施AdapterView.OnItemClickListener.

More info can be found here http://developer.android.com/guide/topics/ui/binding.html.

更多信息可以在这里找到http://developer.android.com/guide/topics/ui/binding.html

Note this is used for each row in the ListView and not the individual TextView or ImageView in each row.

请注意,这用于 ListView 中的每一行,而不是每行中的单个 TextView 或 ImageView。