Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:17:03+00:00 2026-06-07T00:17:03+00:00

My situation: an activity with a button, a checkbox checkAll and a custom ListView

  • 0

My situation: an activity with a button, a checkbox checkAll and a custom ListView with image,name,surname and checkbox

Problem: when i click on checkAll, the listView doesn’t update the vale of each checkbox in the listView(whic have to be all checked).

Here there is my MainActivity with the listView:

private final static int INFO_DIALOG = 1;
private ListView mList;
private CheckBox checkAll;
private Button buttonCreaPartita;
private Person[] people;
private int peopleSize;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout_select_friend);      

     mList= (ListView) findViewById(R.id.list);  //the listView 

     NewQAAdapterSelectFriends adapter= new NewQAAdapterSelectFriends(this); 
     //my adapter
     MainActivity.this.people = new Person[]{ //add person to my array of Person
            new Person(1,"Belen", "Rodriguez", R.drawable.belen,false),
            new Person(2,"Cameron", "Diaz", R.drawable.cameron,false),
            new Person(3,"Jessica", "Alba", R.drawable.alba,false),
            new Person(4,"Yolanthe", "Cabau", R.drawable.cabau,false), 
            new Person(5,"Belen", "Rodriguez", R.drawable.belen,false),
            new Person(6,"Cameron", "Diaz", R.drawable.cameron,false)
            };

     peopleSize=people.length;
     adapter.setData(people); //pass the array of person to my adapter
     mList.setAdapter(adapter);   //set the adpater   

    checkAll =(CheckBox)findViewById(R.id.checkBoxAll);//my CheckAll  checkbox
    checkAll.setChecked(false);//default value

    checkAll.setOnClickListener(new OnClickListener() { 
        @Override
        public void onClick(View v) {

            if((checkAll.isChecked())){

                for(int i=0;i<peopleSize;i++){
                    people[i].setCheck(true); 
                                   //update the new value on the array
                   ((BaseAdapter) mList.getAdapter()).notifyDataSetChanged() ; //i'll try to refresh the value of checkbox in the listview
                 }
            }

            else if(!(checkAll.isChecked())){
                for(int i=0;i<peopleSize;i++){                                                people[i].setCheck(false);
                                                ((BaseAdapter) mList.getAdapter()).notifyDataSetChanged() ;
                        }       
            }
        }
    });        
}


  buttonNext=(Button)findViewById(R.id.button);

    buttonNext.setOnClickListener(new OnClickListener() {
    //after user select the person on the list press buttonNext to go to next Activity
        @Override
        public void onClick(View arg0) {
            Person[] selectedFriends=new Person[peopleSize];
            int countSelected=0;
            for(int i=0;i<peopleSize;i++){
                if(people[i].isCheck()){            
                    selectedFriends[countSelected]=people[i];
                    countSelected++;
                }
            }
            Toast.makeText(getApplicationContext(), "Numero elemento:"+countSelected, Toast.LENGTH_SHORT).show();
            Intent intentMenu=new Intent(getApplicationContext(), MenuActivity.class);
            Toast.makeText(getApplicationContext(), "Partita Creata", Toast.LENGTH_LONG).show();
            startActivity(intentMenu);
        }
    });

This is my adapter with the getView function():

    public class NewQAAdapterSelectFriends extends BaseAdapter {
private LayoutInflater mInflater;
private Person[] data;
boolean[] checkBoxState;
ViewHolder viewHolder;

public NewQAAdapterSelectFriends(Context context) { 
    mInflater = LayoutInflater.from(context);
}


public void setData(Person[] data) {
    this.data = data;
    checkBoxState=new boolean[data.length];
    for(int i=0;i<data.length;i++){
        checkBoxState[i]=data[i].isCheck();
    }
}

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

@Override
public Object getItem(int item) {
    return data[item];
}

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



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item_select_friends, null);
        viewHolder=new ViewHolder();

        viewHolder.nameText=(TextView) convertView.findViewById(R.id.personName);
        viewHolder.surnameText=(TextView) convertView.findViewById(R.id.personSurname);
        viewHolder.contactImage=(ImageView) convertView.findViewById(R.id.personImage);
        viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);

        convertView.setTag(viewHolder);

    }
    else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.nameText.setText(data[position].getName());
    viewHolder.surnameText.setText(data[position].getSurname());
    viewHolder.contactImage.setImageResource(data[position].getPhotoRes());
    viewHolder.contactImage.setScaleType(ScaleType.FIT_XY);
    viewHolder.checkBox.setChecked(checkBoxState[position]);
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
               if(((CheckBox)v).isChecked()){
                   checkBoxState[position]=true;
                   data[position].setCheck(true);
               }else{
                   checkBoxState[position]=false;
                   data[position].setCheck(false);
               }
            }
        });
    viewHolder.checkBox.setChecked(checkBoxState[position]);
    return convertView;
}


static class ViewHolder {
    TextView nameText;
    TextView surnameText;
    ImageView contactImage;
    CheckBox checkBox;
    CheckBox checkAll;
}
}

With log i’m sure that when i click on checkAll all my items change their value in true (checked), but this is not show on the relative checkbox that stay all unchecked…

Thanks for the answer!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:17:05+00:00Added an answer on June 7, 2026 at 12:17 am

    I’ll suggest you to pass Person [] through your Adapter‘s constructor like this:

    public NewQAAdapterSelectFriends(Context context, Person[] p) { 
        mInflater = LayoutInflater.from(context);
        this.data = p; //set class level variable.
    }
    

    wipe out all boolean[] checkBoxState; from adapter, since you have state reference in person class i.e people.setCheck(true);. so instead of using checkBoxState use data[position].getCheck() like this:

    viewHolder.checkBox.setChecked(data[position].getCheck()); //to set check box state.
    

    Remove setData(Person[] data) method too, and hope it all will go well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my situation. What i have: an activity with a checkbox(called checkAll) at
I have this situation where i have to start an activity from my mainActivity.
This is my situation. I have two activities: ONE and TWO. In TWO activity
Situation: A custom Master Document content type inherits from Document The Master Document content
I've heard that pressing the back button will essentially cause the current Activity to
Will many import statements in a activity/apps affect performance in android. Example situation 1:
im stuck in a situation where i am switching from activity 1 to activity
I am facing a situation in which I have a ListView item. When I
Situation: I have a ListView with my own ListAdapter . In every row I
I have the following situation: one activity (DateActivity) calls another activity (ListActivity) when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.