简体   繁体   中英

Add onClickListener to listview item

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.

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

You can use mStrings and mImages in your OnItemClickListener . Assumed from your LazyAdapter that they are arrays. Maybe you can try something like this.

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

What may help you is implementing an AdapterView.OnItemClickListener .

More info can be found here 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM