0

I am trying to create a GridView that contains some buttons. The buttons are getting populated but the OnItemClickListener() is not getting invoked when a Button inside the GridView is clicked.

Activty Class Code

gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ButtonAdapter(getApplicationContext(),answer));
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            flipper.showNext();

        }
    });

Custom Button Adapter

public class ButtonAdapter extends BaseAdapter{

private Context context;
private int[] numbers = new int[100];
private String[] answer;

public ButtonAdapter (Context context,String[] answer){
    this.context = context;
    this.answer = answer;
    for(int i=0;i<100;i++){
        numbers[i] = i+1;
    }
}

@Override
public int getCount() {
    return numbers.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    View gridView;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(view == null) {
        gridView = new View(context);
        gridView = inflater.inflate(R.layout.adapter, null);

    } else {
        gridView = (View) view;
    }

    Button button = (Button) gridView.findViewById(R.id.adapter_button);
    button.setText(numbers[i]+"");

    if(answer[i].equals("null"))
        button.setBackgroundColor(context.getResources().getColor(R.color.reset));
    else
        button.setBackgroundColor(context.getResources().getColor(R.color.optionCheck));

    return gridView;
}
}

adapter.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/adapter_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

acitvity_layout.xml

<GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:numColumns="5"
        android:columnWidth="50dp"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/back"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
Vipul Behl
  • 644
  • 1
  • 7
  • 20
  • You're hooking to the gridView's setOnItemClickListener, surely that should be the buttons' setOnItemClickListener? – JeffUK Jul 09 '17 at 17:31
  • 1
    I am following the android docs (https://developer.android.com/guide/topics/ui/layout/gridview.html). Here setOnItemClickListener is set on to the gridview rather than the element present inside it. – Vipul Behl Jul 09 '17 at 17:40
  • @JeffUK His buttons lie inside the gridview which he populates by adapter.xml. I am skeptical about his getView implementation where he makes a new View and returns it. Why not return that same View he passed in arguments! – Pushan Gupta Jul 09 '17 at 17:42
  • Try implementing onItemClickListener and implementing the onItemClick inside an overridden function. Also are you sure about your flipper.showNext() method? – Pushan Gupta Jul 09 '17 at 17:48
  • Yes the flipper.showNext() method is fine. Earlier I populated the gridview using just textviews without using the custom adapter. That time the clicks on individual elements were working. – Vipul Behl Jul 09 '17 at 17:50

1 Answers1

0

You need to implement an interface and have your Activity subscribe to the specific button presses via the interface listener. Your ButtonAdapter should implement the onClickListener, but your Activity should implement the interface with your custom methods. The Activity and the Adapter are two different worlds with two different scopes. Go through this post slowly and follow its logic. It should set you in the right direction: How to create interface between Fragment and adapter?

The way an adapter works with a Fragment is virtually the same as it does with an Activity.

Mark Filter
  • 353
  • 5
  • 8
  • ok, that might be a way of doing it. Currently what I am looking for is what's wrong with the code I have here. – Vipul Behl Jul 09 '17 at 17:59
  • Well, for starters, you are setting an onClickListener for the GridView...not the button. Furthermore, without using an interface, you won't be able to register any UI interactions to the Activity. I would recommend you use an interface. – Mark Filter Jul 09 '17 at 18:27
  • yes, that was the thing I was doing wrong. I set OnClickListener on button inside adapter and it started working. – Vipul Behl Jul 10 '17 at 04:10