How to delete multiple selected line items from a list?

advertisements

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();
        }

    }   

}


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");
        }
    }
}