Android - Gridview with Custom BaseAdapter, create onclicklistener [duplicate]

孤街醉人 提交于 2019-12-01 15:48:33

The problem in your case is that the button is consuming the click and it doesn't get to GridView. I see two options:

  1. The easy one: in cell.xml have these properties for your Button: android:clickable="false" and android:focusable="false"
  2. A grosso-modo: send the Activity as parameter to adapter and expose a public method from MainActivity. If you think of re-using the adapter, then send an abstract type of activity or an interface.

Something as:

public class MainActivity extends Activity {
    private TextView text;
    private GridView gridView;
    private final String[] items = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.feedback);

        gridView = (GridView) this.findViewById(R.id.myGridView);
        CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items);
        gridView.setAdapter(gridAdapter);

    }

    public void itemClicked(int position) {
        text.setText(items[position]);
    }
}

and in your adapter:

public class CustomGridAdapter extends BaseAdapter {

    private MainActivity context;
    private String[] items;
    LayoutInflater inflater;

    public CustomGridAdapter(MainActivity context, String[] items) {
        this.context = context;
        this.items = items;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.cell, null);
        }
        Button button = (Button) convertView.findViewById(R.id.grid_item);
        button.setText(items[position]);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                context.itemClicked(position);
            }
        });

        return convertView;
    }

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

    @Override
    public Object getItem(int position) {
        return items[position];
    }

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

Actually for gridview setOnItemClickListener you are not doing anything wrong. It's the cell view that is creating the problem. Try changing cell-view button to a textview, it should work. Right now when you tap on an item in a gridview the item is not actually clicked because your cell-view button has it's own implementation of OnClickListener, and that is why setOnItemClickListener is not getting called.

You can use the setTag() adnd getTag() which allow you to put/get any type of object. So in the future, you will be able to pass anything (if you make the appropriate cast after)

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

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell, null);
    }

    Button button = (Button) convertView.findViewById(R.id.grid_item);
    button.setText(items[position]);
    convertView.setTag(items[position]); 
    return convertView;
}

AND

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String str = (String) view.getTag();
            text.setText(str);
            Log.i("ITEM_CLICKED", "" + (String) (gridView.getItemAtPosition(position)));
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!