user1
user1

Reputation: 87

How to get the two check box values in android

enter image description hereHi in the below code list of users with checkboxes.when I am selecting first user it's displaying correctly and same as second users also.

Now I want to return at a time to return first user and second user these two values I want to store into one variable.

java

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

    }

Upvotes: 0

Views: 39

Answers (1)

kodartcha
kodartcha

Reputation: 1073

To get the selected items in your 'ListView' use this:

List<String> checkUsers = new ArrayList<String>();
SparseBooleanArray checkedPositions = listView.getCheckedItemPositions();
for (int i = 0; i < yourListView.getCount(); i++) {
     if (checkedPositions.get(i) == true) {
         checkUsers.add(yourListView.get(i));
     }
}

Then you have all the checked users from your 'ListView' in a List. If you are able to use the 'ListView' for your purposes then ok. If not, you could add all the items in a string separated by a character, for example "-" like this:

String users = "";
for(int i = 0; i<checkUsers.size(); i++){
   if(i = 0){
       users = checkUsers.get(i);
   }else{
       users = users + "-" + checkUsers.get(i);
   }
}

And you will have all the users in the same string separated by "-". This way you will be able the split the users again and use the one by one.

Hope it helps! Good Luck!

Upvotes: 1

Related Questions