0

I want to select all CheckBox from Custom ListView on single Button click .

But when I have more than 9 items in ListView I am getting NullPointerException in below second line of code.

View vi= diffeneceLv.getChildAt(i);
CheckBox cb = (CheckBox) vi.findViewById(R.id.conschkbx);
1
  • Could you post an SSCCE? That should make it easier for us to help you. Commented Dec 31, 2012 at 10:27

2 Answers 2

3

You are getting it wrong, ListView re-uses your rows, which means number of created rows/layouts in memory are not equal to your items in array.

Typically ListView re-sets the new data to previous row upon scroll.

I would suggest you to study this blog post, here the author is maintaing the Checked state and then setting it accordingly in getView() of adapter.

The author have created an array of bolean like this:

private boolean[] thumbnailsselection;

and storing the state of check or uncheck, and later accessing it from getView(), what you will do is, you will store true for all index and refresh your adapter. It'll select all your rows.

Here is another post.

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

Comments

0

You should NOT hold references of individual views for this purpose, as they are recycled.

For your convenience, ListView holds a BooleanSparseArray to store what items are checked. This array contains a map of item id (index/position of items in adapter) to a boolean value.

Since ListView does all that for you, its good to avoid re-inventing the wheel and use ListView's capability to hold checked state of its items. All you have to do is to set a choice mode for ListView: setChoiceMode(int choiceMode)

1 Comment

Actually i want to add SelectAll button in layout with custom list view. on click on SelectAll button all checkbox from listview will get checked. but if i have lesser than 9 rows in listview it works proper else gives null pointer exception for checkbox.

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.