• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ListView. Getting itemId when click on imageView

vovs

Newbie
Hello, guys!

In my ListView each item consists of ImageView and TextView. I want when I click on image - get itemid(and then show image in dialog).

How to catch click on ImageView and get Id of ListItem?
lv_240.jpg



Code:
ImageView image;

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

            contactsList=(ListView)findViewById(android.R.id.list);
            contactsList.setOnItemClickListener(clickListener);

            image = (ImageView)findViewById(R.id.image_a);
            image.setOnClickListener(imListener);

        }

        OnItemClickListener clickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long itemId) {            
            Toast.makeText(getApplicationContext(), "listItem " + itemId, Toast.LENGTH_SHORT).show();

            }
        };

        OnClickListener imListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                   Toast.makeText(getApplicationContext(), "Image of listItem ", Toast.LENGTH_SHORT).show();

            }
        };
 
There are two things you can do.

The first way is to define an OnTouchListener on the ImageView. Just return false here. If your return false you indicate that you didn
 
thanks, JordiDroid..

It is easy..

I must write class, that implements OnClickListener in my ListAdapter:

Code:
class youaddaper extends BaseAdapter{

   public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflate = LayoutInflater.from(context);
        View v = inflate.inflate(id, parent, false);

      ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
        imageview.setOnClickListener(new imageViewClickListener(position));
      //you can pass what ever to this class you want,
      //i mean, you can use array(postion) as per the logic you need to implement 
   }
   class imageViewClickListener implements OnClickListener {
   int position;
    public imageViewClickListener( int pos)
        {
            this.position = pos;
        }

    public void onClick(View v) {
      {// you can write the code what happens for the that click and 
       // you will get the selected row index in position
     }
}
 
Back
Top Bottom