如何在Android中使用自定义适配器获取多个复选框值 [英] how to get the the multiple checkbox values using custom adapter in android

查看:39
本文介绍了如何在Android中使用自定义适配器获取多个复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我将显示带有复选框的textview.现在,当我选择单个值时,它还将返回与第二个textview名称相同的textview名称.

Hi In the below code I am display textview with checkboxes.Now When I am selecting single value it's returning the name of the textview same as second textview name also.

现在,我想一次基于复选框选择的值返回多个textview名称.例如:假设我选择了两个用户user1,假设我选择了user2这两个我想在单个变量中返回这两个名称.

Now I want to return at a time multiple textview names based on checkbox selected values. Eg:suppose two users I am selecting user1,user2 these two I got selected I want to return these two names in single variable.

java

  public class GroupList extends ListActivity 
{

    boolean[] checkBoxState;
    boolean isChecked;
    String check;
    ListView users;
    int position;
    private IAppManager imService = null;

    private FriendListAdapter friendAdapter;

    public String ownusername = new String();

    private class FriendListAdapter extends BaseAdapter 
    {   
        @SuppressWarnings("unused")

        class ViewHolder {
            TextView text;
            ImageView icon;
            CheckBox check1;



        }

        private LayoutInflater mInflater;
        private Bitmap mOnlineIcon;
        private Bitmap mOfflineIcon;        

        private FriendInfo[] friend = null;





        public FriendListAdapter(Context context) {
            super();            

            mInflater = LayoutInflater.from(context);

            mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
            mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

        }

        public void setFriendList(FriendInfo[] friends)
        {
            this.friend = friends;

        }


        public int getCount() {     

            return friend.length;
        }


        public FriendInfo getItem(int position) {           

            return friend[position];
        }

        public long getItemId(int position) {

            return 0;
        }

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

            final ViewHolder holder;


            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.grouplist, null);


                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                holder.check1 = (CheckBox)convertView.findViewById(R.id.checkBox1);

                convertView.setTag(holder);

            }           

            else {

                holder = (ViewHolder) convertView.getTag();

            }


            holder.text.setText(friend[position].userName);
            holder.icon.setImageBitmap(friend[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);


            checkBoxState = new boolean[friend.length];
            holder.check1.setChecked(checkBoxState[position]);
            holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkBoxState[position]=isChecked;

                    if(isChecked){

                    check=friend[position].userName;


                    }


                    Toast.makeText(getApplicationContext(),friend[position].userName+"checked", Toast.LENGTH_LONG).show();
                }
            });


            return convertView;
        }

    }

推荐答案

在适配器中声明并初始化...

Declare and Initialize this in your adapter...

ArrayList<String> checkedFriends = new ArrayList<String>();

以此替换您的点击列表....

Replace your click listner with this....

holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkBoxState[position]=isChecked;

                    if(isChecked){
                        checkedFriends.add(friend[position].userName);
                        check=friend[position].userName;
                    } else {
                        checkedFriends.remove(friend[position].userName);
                    }
                    Toast.makeText(getApplicationContext(),checkedFriends.toString()+" checked", Toast.LENGTH_LONG).show();
                }
            });

这篇关于如何在Android中使用自定义适配器获取多个复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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