0

I have implemented a simple method to delete multiple items from a listview following this solution Removing muliple items from listview using Check box in Android, then modified it a small bit to allow for a switch statement for the two button click events, add & delete.But the problem is when I click the delete button the app crashes giving me these errors: http://pastebin.com/2NmCQk2B

I'm not sure why I'm getting the null pointer exception as I believe I have assigned everything correctly.

Can someone better explain why I could be getting this error? Or perhaps a better way of deleting selected line items from a listview?

The complete class is posted below for better understanding:

public class TopRatedFragment extends Fragment implements OnClickListener {

    ListView mListView;
    EditText mValue;
    Button mAdd,mDel;
    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    SparseBooleanArray mCheckStates ;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
         mAdd = (Button)rootView.findViewById(R.id.newList);
         mDel = (Button)rootView.findViewById(R.id.delBtn);
         mAdd.setOnClickListener(this);
         mDel.setOnClickListener(this);
         mValue = (EditText)rootView.findViewById(R.id.listData);
         adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, list);

    // set the lv variable to your list in the xml
         mListView=(ListView)rootView.findViewById(R.id.listView);  
         mListView.setAdapter(adapter);


        return rootView;    
    }


    public void onClick(View v)
    {
        switch(v.getId()){
        case R.id.newList:
             //DO something
            String input = mValue.getText().toString();
            if(input.length() > 0)
            {
                // add string to the adapter, not the listview
                adapter.add(input);
                // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
            }
        break;
        case R.id.delBtn:
             //DO something
            SparseBooleanArray checked = mListView.getCheckedItemPositions();
            for (int i = 0; i < mListView.getCount(); i++){

                //line 65
                if (checked.get(i)==true)
                {
                     list.remove(i);

                } 
                adapter.notifyDataSetChanged(); 

            }
             mListView.clearChoices();               
        }


    }   

}

Listview/Interface

Community
  • 1
  • 1
Brian Var
  • 6,029
  • 25
  • 114
  • 212

3 Answers3

1

Remove from adapter instead from listview try following:

 SparseBooleanArray checked = mListView.getCheckedItemPositions();

  if(checked!=null){
  for (int i = 0; i < mListView.getCount(); i++){

            //line 65
            if (checked.get(i)==true)
            {
                 adapter.remove(mListView.getItemAtPosition(i));

            } 
            adapter.notifyDataSetChanged(); 

        }
         mListView.clearChoices(); 
   }

getCheckedItemPositions Returns: A SparseBooleanArray which will return true for each call to get(int position) where position is a checked position in the list and false otherwise, or null if the choice mode is set to CHOICE_MODE_NONE.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
1

You need to use valueAt(), the working code should be

SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();
if (checkedItems != null) {
    for (int i=0; i<checkedItems.size(); i++) {
        if (checkedItems.valueAt(i)) {
            String item = mListView.getAdapter().getItem(
                                  checkedItems.keyAt(i)).toString();
            Log.i(TAG,item + " was selected");
        }
    }
}
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
0

I used loop this way, and it worked:

SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                int itemCount = getListView().getCount();

                for(int i=itemCount-1; i >= 0; i--){
                    if(checkedItemPositions.get(i)){
                        adapter.remove(list.get(i));
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();
Rafael
  • 6,091
  • 5
  • 54
  • 79