1

Hi In the below code by using ischecked I want to return the array of checkbox values I want to return.

But How to pass the parameter only checked values in creategroup method.Instead of check which string I have to pass as a parameter.

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

        @SuppressWarnings("unused")
        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);

            final ArrayList<String> checkedFriends = new ArrayList<String>();
            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;

                    } 

                }
            });


            return convertView;
        }
        public ArrayList<FriendInfo> getCheckedItems(){
            ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();

            if(checkBoxState!=null){
               for (int i = 0; i < checkBoxState.length; i++) {
                   if(checkBoxState[i]){
                       result.add(getItem(i));
                   }
               }
            }
            return result;
        }
    }
    protected void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        setContentView(R.layout.group_list_screen);

        Button create=(Button)findViewById(R.id.create);

        create.setOnClickListener(new OnClickListener() {

            @SuppressWarnings({ "unused", "unchecked" })
            @Override
            public void onClick(View v) {
                String groupname = getIntent().getStringExtra("nick");

                            try {

                                String result1 = imService.CreateGroup(groupname,imService.getUsername(),check);
                            } catch (UnsupportedEncodingException e) {

                                e.printStackTrace();
                            }



                Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();

            }
        });


        friendAdapter = new FriendListAdapter(this); 
        friendAdapter.getCheckedItems();



    }
1
  • why cannot you use a tag on the view and do what you need?Comment if that line does not make sense.I shall add the code. Edit:I've read your question a couple of times now.I am unable to figure out what exactly do you need. Commented Feb 3, 2015 at 8:40

1 Answer 1

1

You can add this method to your adapter:

public ArrayList<FriendInfo> getCheckedItems(){
    ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();

    if(checkBoxState!=null){
       for (int i = 0; i < checkBoxState.length; i++) {
           if(checkBoxState[i]){
               result.add(getItem(i));
           }
       }
    }
    return result;
}

To call it: friendAdapter.getCheckedItems();

UPDATE :

So your code will be like that:

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

            @SuppressWarnings("unused")
            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);

                final ArrayList<String> checkedFriends = new ArrayList<String>();
                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;

                        } 

                    }
                });


                return convertView;
            }
            public ArrayList<FriendInfo> getCheckedItems(){
                ArrayList<FriendInfo> result = new ArrayList<FriendInfo>();

                if(checkBoxState!=null){
                   for (int i = 0; i < checkBoxState.length; i++) {
                       if(checkBoxState[i]){
                           result.add(getItem(i));
                       }
                   }
                }
                return result;
            }
        }

protected void onCreate(Bundle savedInstanceState){       
            super.onCreate(savedInstanceState);

            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
            setContentView(R.layout.group_list_screen);

            Button create=(Button)findViewById(R.id.create);


            friendAdapter = new FriendListAdapter(this); 
            /*
             * here you must load list items before calling 
             * "getCheckedItems()", I think you use "setFriendList(...)" for that 
             * so you need to do friendAdapter.setFriendList(YOUR_LIST). Otherwise you will have an empty array 
             * because there is no items, so no checked items too.
             */
            friendAdapter.getCheckedItems();

            create.setOnClickListener(new OnClickListener() {

                @SuppressWarnings({ "unused", "unchecked" })
                @Override
                public void onClick(View v) {
                    String groupname = getIntent().getStringExtra("nick");

                                try {
                                    /* 
                                     * you can also call "getCheckedItems()" here,
                                     * to send the checkedItems to "imService"
                                     */
                                    String result1 = imService.CreateGroup(groupname,imService.getUsername(),check);
                                } catch (UnsupportedEncodingException e) {

                                    e.printStackTrace();
                                }



                    Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();

                }
            });    

        }
Sign up to request clarification or add additional context in comments.

6 Comments

String result1 = imService.CreateGroup(groupname,imService.getUsername(),check); insted of check how to pass the only checked values from friend list
The method above will return only the checked values from friends list ( values that have checkBoxState[i] == true). Use this method to get the list of friends (it return an ArrayList<FriendInfo>) and pass it to imService.CreateGroup() method. --- Mention that friendAdapter must be created before create.setOnClickListener()
friendAdapter.getCheckedItems(); it's giving NPE
Because checkBoxState[] is null, You must call getCheckedItems(); after creating the adapter (friendAdapter = new FriendListAdapter(this)) and putting your objects on it. Read my Mention in the comment above.
see my code Main question is after selecting two friends in the friends I want to pass as a paramter using like this String result1 = imService.CreateGroup(groupname,imService.getUsername(),check); insted of check I want to pass the string array that is which string[] name I want to pass to this .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.