0

I have a list view which contains an item name and checkbox in front of the item name, and one delete button below the layout, if my list contains 15 items then only 10 items are visible, when I scroll down and check the 15th item and then press the delete button, it will throw a null-pointer-exception on the check box. The following is my code to delete items from list view using a check box.

deleteButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        System.out.println("Cursor length is " + cursor.getCount());
        for (int i = 1; i <= cursor.getCount(); i++) {
            listData.recomputeViewAttributes(view);
            listData.getResources();
            listData.smoothScrollToPosition(i);
            view = listData.getChildAt(i-1);

            System.out.println("Count is "+i);
            CheckBox box = (CheckBox) view.findViewById(R.id.checkBox1);
            if (box.isChecked()) {

                System.out.println("Box Checked");

                /*cursor.moveToPosition(i);
                int id = cursor.getInt(0);
                System.out.println("Id is " + id);
                controller.deleteBookmarkOnId(id);*/
            }
        }
        dialog.dismiss();
    }
});
JJD
  • 50,076
  • 60
  • 203
  • 339
  • check this http://stackoverflow.com/questions/18715556/removing-muliple-items-from-listview-using-check-box-in-android/18716460#18716460 – Raghunandan Sep 13 '13 at 08:01

1 Answers1

0

The findViewById method should not take in input a "position" (like i - 1), but an ID from your resource package, like R.id.check_box.

Paolo M
  • 12,403
  • 6
  • 52
  • 73