Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old April 19th, 2011, 12:09 PM   #1 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Feb 2011
Location: Ukraine, CK
Posts: 31
 
Device(s): Galaxy Tab 10.1 Galaxy Nexus
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via ICQ to vovs Send a message via Skype™ to vovs
Default ListView. Getting itemId when click on imageView

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?



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

            }
        };

vovs is offline  
Reply With Quote
sponsored links
Old April 20th, 2011, 04:14 AM   #2 (permalink)
Junior Member
 
Join Date: Apr 2011
Location: Netherlands
Posts: 38
 
Device(s): Samsung Galaxy S, HTC Desire, Samsung Galaxy Tab 10.1v
Carrier: Not Provided

Thanks: 0
Thanked 2 Times in 2 Posts
Default

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īt handle the request so it will be send to the parent of this ImageView. Now you handle the touch event in the ListItem where you can call the getId() method.

The second thing you can do is define an OnClickListener on generation of each child that passes the event to the parent by calling performClick() on the parent. Then you can handle the event from the ListItem.
JordiDroid is offline  
Reply With Quote
Old April 20th, 2011, 01:43 PM   #3 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Feb 2011
Location: Ukraine, CK
Posts: 31
 
Device(s): Galaxy Tab 10.1 Galaxy Nexus
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via ICQ to vovs Send a message via Skype™ to vovs
Default

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
     }
}
vovs is offline  
Last edited by vovs; April 20th, 2011 at 01:47 PM.
Reply With Quote
Reply
Tags
listview


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:30 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.