How to add gridview setOnItemClickListener

亡梦爱人 提交于 2019-11-29 03:53:41
Jack K Fouani

You are requesting click on the item(not on the button inside the item) so need to change your child XML layout

from

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:textStyle="bold"
    android:text="Code\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

</RelativeLayout>

to

<?xml version="1.0" encoding="utf-8"?>
<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/city"
    android:layout_width="183dp"
    android:layout_height="90dp"
    android:textSize="19sp"
    android:clickable="true"
    android:textStyle="bold"
    android:text="Code\n\nCity"
    android:gravity="center"
    android:background="@drawable/city_btn_tablet" />

or you can add ClickListener inside adapter

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

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    gridView =new View(mContext);

    gridView = inflater.inflate(R.layout.gridview_members, null);

    Button city = (Button) gridView.findViewById(R.id.city);

                city.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    //Toast here

        }});

    return gridView;
}
Mohammed Saleem

GridView is clickable no need to put button inside GridView.

To add listener to the grid add the following code:

// Implement On Item click listener
gridView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
            case 0: 
                break;
            case 1:
                break;

        }
    }
}); 

And so on you can add all numbers in the cases.

If you want to get text that clicked item you can use this

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String item = ((TextView)view.findViewById(R.id.ID_OF_TEXTVIEW)).getText().toString();
                Toast.makeText(showMissions.this, "" + item, Toast.LENGTH_SHORT).show();

            }
        });
Dilip Malviya

I am suggesting you to add click listener in getview method rather than adding in activity class. try this code

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

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

gridView =new View(mContext);

gridView = inflater.inflate(R.layout.gridview_members, null);

Button city = (Button) gridView.findViewById(R.id.city);

city.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
            //Do your task here
        }
    });
    return gridView;
}

In your CustomAdapter into

 onBindViewHolder(...
{
  yourViewHolder.gridview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        //Do Something
       }
    });

}

You can add click listener this way to your grid view items in Kotlin

grdlyt_merchant_color_code.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            loyaltyColor = parent.getItemAtPosition(position) as LoyaltyColor?
            colorCode= LoyaltyUtils.getLoyaltyColorById(loyaltyColor!!.colorId).colorCode
            val color = ContextCompat.getColor(requireContext(), colorCode!!)
            shape.setColor(color)
            loyalty_card_img.background = shape
            uncheckAllColors()
            loyalty_card_img.visibility = View.GONE
            loyaltyColor!!.isColorSelected = true

            mAdapter.notifyDataSetChanged()
        }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!