Android的 - GridView的,自定义布局onclicklistener [英] Android - Gridview, custom layout onclicklistener

查看:372
本文介绍了Android的 - GridView的,自定义布局onclicklistener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部的两份TextViews,当填充GridView控件,一个OnClickListener设置,返回所选定的项目的位置的GridView。我想当选择TextViews之一来触发的方法。

这可能吗?如果是的话,我该如何设置呢?

编辑3

在我的活动填充界河在GridView:我检索字符串数组从我的strings.xml ,一循环检查数组和搜索基于项目的共享preferences内部名称的条件里面的每个项目,这个循环完全是为了计算有多少真的条件有,所以它会检索一个 INT 保存在计数。然后,新的的String [] 创建,这需要一个确切的长度被赋予之前的项目可以添加到它,所以我检查计数如果是大于0,它将给的String [] 的长度计数,然后另一个循环将每个真正添加到的String [] 我们刚刚创建的列表。如果计数 0(第一中的for循环无真实情况),那么只有1个项目被添加到的String [] 和被赋予值无添加收藏​​。

然后,你必须在GridView的OnItemClickListener()。

 的String [] = getResources()getStringArray(R.array.FullList)。
        诠释计数= 0;
        的for(int i = 0; I< s.length;我++){
            。共享preferences SP = getActivity()getShared preferences(最爱,MODE_PRIVATE);
            布尔B = sP.getBoolean(S [I]中,假);
            如果(二==真){
                算上++;
            }
        }
        的String [] newList;
        如果(计数大于0){
            newList =新的String [统计]
            计数= 0;
            的for(int i = 0; I< s.length;我++){
                。共享preferences SP = getActivity()getShared preferences(最爱,MODE_PRIVATE);
                布尔B = sP.getBoolean(S [I]中,假);
                如果(二==真){
                    newList [计数] = S [I]
                    算上++;
                }
            }
        }其他{
            newList =新的String [1];
            newList [0] =没有收藏添加;
        }        GridView控件FavGV =(GridView控件)getActivity()findViewById(R.id.sexp_fav)。
        FavGV.setAdapter(新Tab01_FavAdapter(getActivity(),newList));
        FavGV.setOnItemClickListener(新AdapterView.OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图为arg0,
                    查看ARG1,INT位置,长ARG3){                //意图I =新意图(getActivity(),波什preview_Gestures.class);
                //i.putExtra(\"position,位置);
                // startActivity(ⅰ);
            }
        });

这就是它填充GridView的活动里面的code。在它的适配器的原始功能的表单:这只是填充(从字符串[]他们的名字)与收藏项目在GridView,并增加了与删除,当pressed,显示举杯一个TextView:删除。

 公共类Tab01_FavAdapter延伸BaseAdapter {    私人语境mContext;
    私人LayoutInflater mInflator;    串mEntries [];
    公共Tab01_FavAdapter(上下文C,的String []项){
        mContext = C;
        mInflator =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mEntries =条目;
    }    @覆盖
    公众诠释的getCount(){
        返回mEntries.length;
    }    @覆盖
    公共对象的getItem(INT位置){
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        如果(convertView == NULL){
            convertView = mInflator.inflate(R.layout.favitemlayout,父母,假);
        }        TextView中TX =(TextView中)convertView.findViewById(R.id.favgridremoveitem);
        OnClickListener OCL =新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(mContext,删除,Toast.LENGTH_SHORT).show();
            }
        };
        tx.setOnClickListener(OCL);        返回convertView;
    }
}


解决方案

我假设你正在使用自定义适配器用于填充此 GridView控件,并通过上下文作为参数传递给构造函数。

在自定义适配器,您应该添加 onClickListeners TextViews 。使用上下文,你可以调用方法从你的活动:

 ((CallingActivityName)上下文).methodYouWishToCall(参数);

这会去里面的 onClickListeners

编辑:增加了一些code:

 公共类MyGridAdapter延伸BaseAdapter {    私人最终名单< MyObjectClass> mEntries;
    私人最终LayoutInflater mInflater;
    私人最终上下文mContext;    公共静态类ViewHolder {
        公众的TextView TX;
    }    公共MyGridAdapter(CallingActivityName背景下,列表< MyObjectClass>项){
        超();
        mEntries =条目;
        mContext =背景;
        mInflater =(LayoutInflater)mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }    @覆盖
    公众诠释的getCount(){
        返回mEntries.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回mEntries.get(位置);
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        最后ViewHolder持有人;        如果(convertView == NULL){            convertView = mInflator.inflate(R.layout.favitemlayout,父母,假);
            持有人=新ViewHolder();            holder.tx =(TextView中)convertView
                .findViewById(R.id.favgridremoveitem);            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }        最后MyObjectClass信息= mEntries.get(位置);        holder.tx.setOnClickListener(新OnClickListener(){                    @覆盖
                    公共无效的onClick(视图v){
                        ((CallingActivityName)mContext).favRemove(信息);
                        notifyDataSetChanged();
                    }
        });        返回convertView;
    }}

因此​​, CallingActivityName 是启动适配器,在那里你需要调用所在的方法活动的名称。 信息在GridView控件的位置位置持有的对象。 MyObjectClass 是列表 mEntries

的对象的类名

I have a GridView with two TextViews inside it, When the GridView is populated, an OnClickListener is set which returns the position of the item that was selected. I want to trigger a method when one of the TextViews is selected.

Is this possible? If yes, how do I set this up?

EDIT 3:

Inside my activity whch populated the GridView: I retrieve a String-Array from my Strings.xml, a for loop examines each item inside the Array and searches for a condition based on the Item's name inside the SharedPreferences, this for loop is solely for counting how many "true" conditions there are, so it retrieves a int saved inside the count. Then a new String[] is created, this required an exact length to be given before items can be added to it, so I check count if it's more than 0, it will give the String[] a length of count and then another for loop will add each true to the String[] list that we just created. If count is 0 (no true conditions found in the first for loop) then only 1 Item is added to the String[] and is given the value "No Favourites Added".

Then you have the GridView's OnItemClickListener().

        String s[] = getResources().getStringArray(R.array.FullList);
        int count = 0;
        for(int i = 0; i < s.length; i++) {
            SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
            Boolean b = sP.getBoolean(s[i], false);
            if (b == true) {
                count++;
            }
        }
        String[] newList;
        if (count > 0) {
            newList = new String[count];
            count = 0;
            for(int i = 0; i < s.length; i++) {
                SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
                Boolean b = sP.getBoolean(s[i], false);
                if (b == true) {
                    newList[count] = s[i];
                    count++;
                }
            }
        } else {
            newList = new String[1];
            newList[0] = "No favourites added";
        }

        GridView FavGV = (GridView) getActivity().findViewById(R.id.sexp_fav);
        FavGV.setAdapter(new Tab01_FavAdapter(getActivity(), newList));
        FavGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView arg0, 
                    View arg1, int position, long arg3) {

                //Intent i = new Intent(getActivity(), PosPreview_Gestures.class);
                //i.putExtra("position", position);
                //startActivity(i);
            }
        });

So that's the code inside the Activity which populates the GridView. The Adapter in it's original, functioning form: This simply populates the GridView with Favourite Items (their names from the String[]) and adds a TextView with "Remove" which when pressed, shows a Toast: "Remove".

public class Tab01_FavAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflator;

    String mEntries[];     
    public Tab01_FavAdapter (Context c, String[] entries) {
        mContext = c;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mEntries = entries; 
    }

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

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

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

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

        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.favitemlayout, parent, false);
        }

        TextView tx = (TextView) convertView.findViewById(R.id.favgridremoveitem);
        OnClickListener oCL = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"Remove",Toast.LENGTH_SHORT).show();
            }
        };
        tx.setOnClickListener(oCL);

        return convertView;
    }
}

解决方案

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.

这篇关于Android的 - GridView的,自定义布局onclicklistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆