Android - Gridview, custom layout onclicklistener

无人久伴 提交于 2019-12-08 03:29:26

I am assuming that you are using a custom adapter for populating this GridView, and passing the Context as an argument to the constructor.

In the custom adapter, you should add onClickListeners to the TextViews. Using the context, you can call methods from your activity:

((CallingActivityName)context).methodYouWishToCall(parameters);

This would go inside the onClickListeners.

Edit: Added some code:

public class MyGridAdapter extends BaseAdapter {

    private final List<MyObjectClass> mEntries;
    private final LayoutInflater mInflater;
    private final Context mContext; 

    public static class ViewHolder {
        public TextView tx;
    }

    public MyGridAdapter(CallingActivityName context, List<MyObjectClass> entries) {
        super();
        mEntries = entries;
        mContext = context;
        mInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mEntries.size();
    }

    @Override
    public Object getItem(int position) {
        return mEntries.get(position);
    }

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

        final ViewHolder holder;

        if (convertView == null) {

            convertView = mInflator.inflate(R.layout.favitemlayout, parent, false);
            holder = new ViewHolder();

            holder.tx = (TextView) convertView
                .findViewById(R.id.favgridremoveitem);

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

        final MyObjectClass info = mEntries.get(position);

        holder.tx.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ((CallingActivityName)mContext).favRemove(info);
                        notifyDataSetChanged();
                    }
        });

        return convertView;
    }

}

So, CallingActivityName is the name of the Activity where you initiate the adapter and where the method you need to call resides. info is the object held at position position of the gridview. MyObjectClass is the class name of the objects in the List mEntries.

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