Nothings happening on clicking items of GridView

夙愿已清 提交于 2019-12-13 06:50:54

问题


I am using GridView for displaying the categories , i have used ImageButton and a Text View in Grid View but I am not able to setOnItemClickListener on those items( Imean nothings Happening on Clicking it) My code is this is My Adapter for GridView

public class MyAdapter extends BaseAdapter{
    private ArrayList<String> listJewel_name;
    private ArrayList<Integer> listJewellery;
    private Activity activity;
    //private LayoutInflater inflater;
    public MyAdapter(Activity activity,ArrayList<String> listJewel_name, ArrayList<Integer> listJewellery) {
        super();
        this.listJewel_name = listJewel_name;
        this.listJewellery = listJewellery;
        this.activity = activity;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listJewel_name.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listJewel_name.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageButton imgViewJewel;
        public TextView txtViewTitle;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.tvtext);
            view.imgViewJewel = (ImageButton) convertView.findViewById(R.id.picture);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listJewel_name.get(position));
        view.imgViewJewel.setImageResource(listJewellery.get(position));

        return convertView;
    }
} 

My activity.class is

GridView gridView;
    Button add;
    private MyAdapter mAdapter;
    private ArrayList<String> listJewel_name;
    private ArrayList<Integer> listJewellery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_jewellery_option);
        prepareList();
         // prepared arraylist and passed it to the Adapter class
        mAdapter = new MyAdapter(this,listJewel_name, listJewellery);

        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
       gridView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                String shri=mAdapter.getItem(position);
                Toast.makeText(SelectJewelleryOption.this, shri , Toast.LENGTH_SHORT).show();
            }
        });
    }

My XML file is

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

<com.myjewelbox.SquareImageButtonView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:scaleType="fitCenter"
    android:clickable="true" />

    <TextView
        android:id="@+id/tvtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="1dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_gravity="bottom"
        android:textColor="@color/Golden"
        android:background="#55000000"
        android:focusable="false"
        />
</FrameLayout>

And I have set this XMl file to my Main XML file's GridView


回答1:


Set ItemClickListener for items of GridView in it's adapter,instead of set an ItemClickListener directly for GridView.

view.imgViewJewel.setItemClickListener(activity);




回答2:


In your adapter class, setOnClickListener on your convertView before return line.

convertView.setOnClickListener(new onClickListener(){
    //In this method you can get the items same as you are getting in your
    //adapter, as follow:-

    String name=listJewel_name.get(position);

    //or you can do whatever you want as now you have the whole view that is 
    //clicked, so you can event get its children or start a new activity or 
    //whatever you want
}
return converView;

Hope this helps.




回答3:


Please change your xml to this-

Add thease 2 line-

android:clickable="true"

android:focusable="false"

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

        <com.myjewelbox.SquareImageButtonView
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="130dp"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/tvtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#55000000"
            android:focusable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="1dp"
            android:paddingTop="10dp"
            android:textColor="@color/Golden" />

    </FrameLayout>

And for more detail see here- http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html




回答4:


Make Sure android:focusable="false" in your First Child of GridView Layout

and

GridViews can't handle clickable items. Button for example won't work. And also if you have any other non-clickable item, you have to make sure that you didn't set the property: clikable="true".

Refer below link - It is same as what you expect

http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html



来源:https://stackoverflow.com/questions/20096558/nothings-happening-on-clicking-items-of-gridview

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