Reputation: 7177
in my android app, i have created 3 grid views dynamically.Hear is the code.and it displayed 3 grid views.
for (int i=0; i<2; i++) {
LinearLayout inflatedView = (LinearLayout)mInflater.inflate(R.layout.library_gallery, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 300);
inflatedView.setLayoutParams(layoutParams);
mGridView = (GridView) inflatedView.findViewById(R.id.library_gallary);
if(Thambs!=null){
mGridView.setAdapter(new LibraryGalleryAdapter(mContext,Thambs1));
setResourse(i,Names,values1);
}
}
Now i want to uniquely identify the click events of these 3 dynamically created grid views. Any idea please?
EDIT:
mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v,final int posision, long id) {
Log.e(TAG, "id : "+v.getId());
}
but it will return same id for each grid view onclick
Upvotes: 0
Views: 5078
Reputation: 11437
Use gridView.setOnItemClickListener. In the click listener, use parent.getId
to find the id of the parent. If you have multiple grid views, you should call gridView.setId()
to set it to something unique.
Upvotes: 1
Reputation: 2628
First of all you need to implement onItemclickListener interface in your class , then you need to check that clicked item is from which grid view by getting Id by view.getId() of clicked view by second argument in onItemClickListener.
then you can identify which view is clicked check that clicked view is childview of GridView by using first argument from OnItemclicked() i.e. parent, check that this parent is which gridview then perform action accordingly for that View.
Hope this explanation works for you..
Upvotes: 0
Reputation: 25873
For each mGridView
you need a separate View.onClickListener
interface implementation, or a single View.onClickListener
implementation and distinguishing each grid with View.getId()
(as vishwa points out). It depends on what you want to do with each grid, and how you want them to behave.
Upvotes: 0