Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

listview - CeckAll button in a list with a lot of elements in Android

This is my situation.
What i have: an activity with a checkbox(called "checkAll") at top-right and below a list of custom element: a TextView and a checkbox.
What i want: when clik on checkAll, then all checkbox of each item in the list are checked.
Problem: All works fine only if all items of the list are visible on the screen. When i have a list with a lot of elements (for example 20), then when click on checkAll my app Crash. LogCat say me: NullPointerException on the line

      CheckBox checkBoxInTheItemList = (CheckBox)
                  mList.getChildAt(i).findViewById(R.id.checkBox);


This is my function:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout_select_friend);  
    checkAll =(CheckBox)findViewById(R.id.checkBoxAll);       
    checkAll.setOnClickListener(new OnClickListener() { 
       @Override
       public void onClick(View arg0) {
          if((checkAll.isChecked())){
           for(int i=1;i<peopleSize;i++){//for each element of my array of Person
             mList=(ListView) findViewById(R.id.list); //This is the id of my List
                                                 //in the list_layout_select_friend
              CheckBox checkBoxInTheItemList = (CheckBox)
              mList.getChildAt(i).findViewById(R.id.checkBox);  //i try to get 
                             //for each item of the List the relative checkBox.                     
        checkBoxInTheItemList.setChecked(true);//Now i checked the checkBox
          }
     }          
    }
    });

}

EDIT: this is my Adapter whith also the method getView

public class NewQAAdapterSelectFriends extends BaseAdapter {
private LayoutInflater mInflater;
private Person[] data;

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

public void setData(Person[] data) {
    this.data = data;
}

@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(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item_select_friends, null);
        final ViewHolder 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);
        viewHolder.nameText.setTag(viewHolder.nameText);
        viewHolder.nameText.setTag(viewHolder.surnameText);
        viewHolder.contactImage.setTag(data[position]);
        viewHolder.checkBox.setTag(data[position]);// is correct this line??????


    } else {


    }

     ViewHolder holder = (ViewHolder) convertView.getTag();
     holder.nameText.setText(data[position].getName());
     holder.surnameText.setText(data[position].getSurname());
     holder.contactImage.setImageResource(data[position].getPhotoRes());
     holder.contactImage.setScaleType(ScaleType.FIT_XY);
     //here i have to write something for the checkbox, right?
     return convertView;
}

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

How i can avoid this problem? How i can check all the checkbox of a big list with invisible elements??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to implement this, you'll need an understanding of how ListView work, namely view recycling. When a list is made in Android, the Views are created dynamically. As you scroll up and down, list items that go off the screen need to have their views wiped clean of data and be repopulated with information for the next item coming in. This happens in the getView method in your List Adapter (which, by the way, would be very helpful for you to post). I believe this is your reason for the Null Pointer Exception. The getChildAt method can only return visible list items, because the others do not exist at the moment. The first visible item will be index 0. So even though your list may have 20 items, if only 5 are visible, only 5 can be returned by getChildAt, and asking for a view that's out of bounds could cause this exception.

The way I would approach your item is by having a data structure that keeps a record of your list items and the state of the check box. A very simple example of this would be an array of booleans, where the true/false value corresponds to if the box is checked. When getView() is called, you can use the position argument a your as the index to your array, and check or uncheck the box based on this. (You probably should be doing something like this anyway, if you're recycling your views properly) If the Check All button is pressed, then just make each element of the array true.

Some sample code to help you out.

private boolean[] isCheckedArray; //Maintains the checked state of your 

... //At some point, probably onCreate or whenever you get your data, initialize your array. If nothing every starts checked off, initialize the whole array to false

checkAll.setOnClickListener(new OnClickListener() { 
   @Override
   public void onClick(View arg0){
       for(int i=1;i<peopleSize;i++)
             isCheckedArray[i] = true;      
   }
});

...
//And finally, in your getView function
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
     ...
     myCheckBox.setChecked(isCheckedArray[position])
     ...
}

Hope this helps. I'd definitely look into view recycling in Android, as it's a key concept and having a good handle on it will save you a lot of time and headache down the road. Good luck.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...