سؤال

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");
    }
});
هل كانت مفيدة؟

المحلول

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!

نصائح أخرى

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"

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"

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!

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.

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;
}

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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top