如何在android中使用自定义适配器获取多个复选框值

时间:2015-01-28 12:56:43

标签: android checkbox

嗨在下面的代码中,我显示带有复选框的textview.Now当我选择单个值时,它还返回textview的名称和第二个textview名称一样。

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

的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;
        }

    }

1 个答案:

答案 0 :(得分:3)

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

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

用这个....替换你的点击监听器。

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();
                }
            });