Anu
Anu

Reputation: 7177

How to handle onclick event of a dynamically created grid view in android?

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

Answers (4)

Code Poet
Code Poet

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

MobileEvangelist
MobileEvangelist

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

m0skit0
m0skit0

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

Vishwanath.M
Vishwanath.M

Reputation: 6317

try using view.getId() inside onItemclick().

Upvotes: 0

Related Questions