Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve the right view?

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My adapter contain the following Views:

private static class ViewHolder {
ImageView p_Image;
TextView p_Name;
TextView p_Price;
TextView p_Psave;
}

How can I know which view from the above 4 was actually pressed (while running OnItemClickListener via GridView)

Thanks
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add a OnItemClickListener on your GridView you will not be able to listen to click events on the individual child components.

You should be able to get a reference to the child views of your parent AdapterView though. onItemClick callback is supplied instances of the parent AdapterView i.e. (GridView) and the view returned by getView() method of the adapter. I think, doing a findViewById() on the view object should give a handle on the individual child components.

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

This Is how I implement onItemClick :



Could you be more specific regarding how should I call getView() ?

Thanks

 
Jarrek Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In get View you bind the layout element to the ViewHolder propertie.

for example : in your viewHolder you have
ImageView img1

in get view you bind the image of the layout to img1

for example : img1= (ImageView) convertView.findViewById(R.id.icon)

then you add an onclick listener in img1

img1.setOnClickListener(new OnClickListener() { ...

that should do the trick.
Setting an on click listener on the gridView isn't precise enought.
You have to set an on click Listener on the view contained in your grid view

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic