Android Listview - can not select multiple items when using a cursor
Asked Answered
F

1

15

I have a listview populated with data from a cursor using SimpleCursorAdapter. I want to make it so I can select multiple items with a checkbox against each item but I can only get it to check a single item at a time i.e. each time I select an item, it will clear the currently selected item.

It works fine if I populate the listview using an ArrayAdapter. I can select multiple items. So I dont know why it doesn't work with the SimpleCursorAdapter.

This is being created in a DialogFragment if that matters.

Really pulling my hair out on this, pleae help!!

Here's the code:

Cursor attributesCursor = mDBHelper.getItemAttributesbyType(menuID, itemID, "M");
getActivity().startManagingCursor(attributesCursor);

ListView lv = new ListView(this.getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
lv.setLayoutParams(params);


SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getActivity(), android.R.layout.simple_list_item_multiple_choice,
                attributesCursor, new String[] { "AttributeDescription" },
                new int[] { android.R.id.text1 },0);


attributesLinearLayout.addView(lv);

lv.setAdapter(adapter);

lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

Edit : Just to add some extra info, the multi choice listview works if i use this code, where "items" is a simple String array:

lv.setAdapter(new ArrayAdapter(this.getActivity(),
            android.R.layout.simple_list_item_multiple_choice, items));

Also, this listview is being dynamically added to an existing Linearlayout (attributesLinearLayout) in a dialogfragment which contains other controls. I also tried extending other adapters, including the array adapter and customer item layouts but that again didnt allow me to select multiple items.

Please help!!

Forsterite answered 30/3, 2012 at 21:12 Comment(3)
did you find any solution, I am also having the same problemAndradite
Why don't you use a Custom Cursor adapter? Or if i were to give you a customadapter with multi-select enabled.Would that solve your issue?Wicket
If it works as an ArrayAdapter, can you just copy your data from the cursor to an Array and then use the array adapter?Cardamom
L
2

I would use the Contextual Action mode in this project if you wish to select multiple items in a listview. This is how it is done.

First of all, the code must extend a ListActivity and implement an ActionMode.Callback

In the onCreate medthod you need to code the following:

@Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "data list goes here" };

    MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
    setListAdapter(adapter);

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        if (mActionMode != null) {
          return false;
        }
        selectedItem = position;

        // Start the CAB using the ActionMode.Callback defined above
        mActionMode = MyListActivityActionbar.this.startActionMode(MyListActivityActionbar.this);
        view.setSelected(true);
        return true;
      }
    });
  }

The you need to call the show method:

private void show() {
    Toast.makeText(MyListActivityActionbar.this, String.valueOf(selectedItem), Toast.LENGTH_LONG).show();
  }

The following needs to be called each time the action mode is shown. It is always called after onCreateActionMode, but may be called multiple times if the mode is invalidated:

  @Override
  public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false; // Return false if nothing is done
  }

Then when the user selects a list item, the following method needs to be called:

  @Override
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuitem1_show:
      show();
      // Action picked, so close the CAB
      mode.finish();
      return true;
    default:
      return false;
    }
  }

Finally when the user exits the selection:

  @Override
  public void onDestroyActionMode(ActionMode mode) {
    mActionMode = null;
    selectedItem = -1;
  }
Lopes answered 27/4, 2015 at 11:45 Comment(7)
I have been able to do it for an array adapter , but for a custom adapter that extends cursor adapter, The actionMode doesnt activateAndradite
Why do you need to use the CursorAdapter?Lopes
As the Data is loaded from a Database.Andradite
Is the database online or called through sqlLite?Lopes
I mean that the app has a database to store incoming and outgoing messages... I use a cursor to get all the required data for the messages from database and show it in a onversation viewAndradite
Then it is n sqllite database. Take a look at this tutorial: vogella.com/tutorials/AndroidSQLite/article.htmlLopes
Let us continue this discussion in chat.Andradite

© 2022 - 2024 — McMap. All rights reserved.