GridView onItemClickListener never gets invoked
Asked Answered
S

7

5

I have a GridView with custom View in it, which is a Button and a TextView. I defined the setOnItemClickListener but it looks like it never invoked, please see peaces of code below.

gridview = (GridView) findViewById(R.id.main_gridview);
gridview.setAdapter(new GridAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) {
        Toast.makeText(getApplicationContext(), "gadsfadsf", 
            Toast.LENGTH_SHORT).show();
        Log.d("Main", "onItemClick");
    }
});
Synge answered 12/5, 2011 at 18:16 Comment(0)
R
3

I had the same issue. While I've not yet figured out why it never gets invoked, I can propose a workaround.

Instead of setting the onClickListener on your GridView, set it on the Button itself inside your GridAdapter, inside your getView() method.

That worked for me!

Ravens answered 12/5, 2011 at 18:23 Comment(1)
Yes it worked , but why touch on grid view not working , this problem do not come with listview why grid viewLaine
N
19

The marked answer is kind of a hack. Instead of setting an onclicklistener to the button just ensure, that the ButtonView and the TextView has the following property:

android:clickable="false"
Novelist answered 4/3, 2012 at 16:46 Comment(2)
and also android:focusable="false"Trot
simple and best answer.Apolitical
R
3

I had the same issue. While I've not yet figured out why it never gets invoked, I can propose a workaround.

Instead of setting the onClickListener on your GridView, set it on the Button itself inside your GridAdapter, inside your getView() method.

That worked for me!

Ravens answered 12/5, 2011 at 18:23 Comment(1)
Yes it worked , but why touch on grid view not working , this problem do not come with listview why grid viewLaine
M
2

It could be that some items in your GridView are stealing focus. Try adding these attributes to any elements you have inside the grid:

android:focusable="false"
android:focusableInTouchMode="false"
Mcclain answered 29/2, 2016 at 13:43 Comment(0)
C
1

Instead of setting the onClickListener on your GridView,
set it on the Button itself inside your GridAdapter, inside your getView() method.

That worked for me!

Clausewitz answered 24/9, 2011 at 9:31 Comment(0)
S
1

I had the same problem, the event grid.itemClickListener was never launched.

In my case I had two listeners: grid.itemClickListener and another clickListener attached to a Button within the item's layout.

After fiddling with the layout for a while, I realized that if there was a widget, within the item's layout, with focusable=true, then itemClickListener was never launched. The clickListener attached to the Button worked well though.

Maybe that was your case. Anyway, I think this information might be useful to other users running into the same problem.

Synoptic answered 4/7, 2013 at 14:10 Comment(0)
M
1

Thanx to CodingUser

what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.

So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)

so what i m trying to say is:

public View getView(int position, View convertView, ViewGroup parent) {

            //Here row is a view and we can set OnClickListener on this
    final View row;
    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        //Here we inflate the layout to view (linear in my case)
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        row = convertView;
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = data.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageBitmap(item.getImage());

    //Now get the id or whatever needed
    row.setId(position);
    // Now set the onClickListener
    row.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Clicked" + row.getId() + "!!",
                    Toast.LENGTH_SHORT).show();
        }
    });
    return row;
}
Militiaman answered 22/1, 2014 at 11:37 Comment(0)
A
0

You can set OnClick for view in Adapter of GridView .It work for me .

public View getView(final int position, View convertView, ViewGroup parent) {
    ObjMenuVideo objListVideo = mListMenuVideo.get(position);
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_video_of_kind, null);
        holder.tv_number_views = (TextView) convertView
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tv_number_views.setText(String.valueOf(objListVideo.getViews()));
    convertView.setId(position);
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent menuVideoIntent = new Intent(mContext,
                    ActivityDetailVideo.class);
            mContext.startActivity(menuVideoIntent);
        }
    });

    return convertView;
}
Arillode answered 30/6, 2015 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.