Implementing a listview with multiple select with filter using a Cursor Adapter
Asked Answered
G

2

8

This problem is discussed in this question Android: Wrong item checked when filtering listview. To summarize the problem, when using a listview with a CursorAdapter and a filter, items selected on a filtered list lose their selection after the filter is removed and instead, items at that position in the unfiltered list get selected.

Using the code sample in the linked question above, where should we put the code to mark the checkboxes. I believe it should be in the getView() method of the CustomCursorAdapter, but I am not sure. Also, how do we access the HashSet holding all the selectedIds in the custom adapter class, because it will be initialized and modified in the main activity holding the list.

My activity implementing the ListView

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.selectfriends);

      Log.v(TAG, "onCreate called") ;

      selectedIds = new ArrayList<String>() ;
      selectedLines = new ArrayList<Integer>()  ;

      mDbHelper = new FriendsDbAdapter(this);
      mDbHelper.open() ;

      Log.v(TAG, "database opened") ;

      Cursor c = mDbHelper.fetchAllFriends();
      startManagingCursor(c);

      Log.v(TAG, "fetchAllFriends Over") ;


      String[] from = new String[] {mDbHelper.KEY_NAME};
      int[] to = new int[] { R.id.text1 };

      final ListView listView = getListView();
      Log.d(TAG, "Got listView");

   // Now initialize the  adapter and set it to display using our row
       adapter =
           new FriendsSimpleCursorAdapter(this, R.layout.selectfriendsrow, c, from, to);

       Log.d(TAG, "we have got an adapter");
     // Initialize the filter-text box 
     //Code adapted from https://mcmap.net/q/150299/-how-to-dynamically-update-a-listview-on-android-closed

       filterText = (EditText) findViewById(R.id.filtertext) ;
       filterText.addTextChangedListener(filterTextWatcher) ;

     /* Set the FilterQueryProvider, to run queries for choices
     * that match the specified input.
     *  Code adapted from https://mcmap.net/q/507493/-how-to-text-filter-an-android-listview-backed-by-a-simplecursoradapter
     */

       adapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                // Search for friends whose names begin with the specified letters.
                Log.v(TAG, "runQuery Constraint = " + constraint) ;
                String selection = mDbHelper.KEY_NAME + " LIKE '%"+constraint+"%'";

                mDbHelper.open(); 
                Cursor c = mDbHelper.fetchFriendsWithSelection(
                 (constraint != null ? constraint.toString() : null));
                return c;
     }
 });




       setListAdapter(adapter);

       Log.d(TAG, "setListAdapter worked") ;


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

       // listView.setOnItemClickListener(mListener);

       Button btn;
       btn = (Button)findViewById(R.id.buttondone);

       mDbHelper.close();

  }


    @Override   
    protected void onListItemClick(ListView parent, View v, int position, long id) {

        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();

        //gets the Bookmark ID of selected position
         Cursor cursor = (Cursor)parent.getItemAtPosition(position);
         String bookmarkID = cursor.getString(0);

         Log.d(TAG, "mListener -> bookmarkID = " + bookmarkID);

         Log.d(TAG, "mListener -> position = " + position);

 //        boolean currentlyChecked = checkedStates.get(position);
 //        checkedStates.set(position, !currentlyChecked);


         if (!selectedIds.contains(bookmarkID)) {

             selectedIds.add(bookmarkID);
             selectedLines.add(position);

         } else {

             selectedIds.remove(bookmarkID);
             selectedLines.remove(position);


             }

     }



  private TextWatcher filterTextWatcher = new TextWatcher() {

      public void afterTextChanged(Editable s)  {

      }

      public void beforeTextChanged(CharSequence s, int start, int count, int after)    {

      }

      public void onTextChanged(CharSequence s, int start, int before, int count)   {
          Log.v(TAG, "onTextChanged called. s = " + s);
          adapter.getFilter().filter(s);
      }
  };

  @Override
  protected void onDestroy()    {
        super.onDestroy();
        filterText.removeTextChangedListener(filterTextWatcher);
  }

My Custom Cursor Adapter:

public class FriendsSimpleCursorAdapter extends SimpleCursorAdapter implements Filterable {

private static final String TAG = "FriendsSimpleCursorAdapter";
private final Context context ;
private final String[] values ;
private final int layout ;
private final Cursor cursor ;

static class ViewHolder {
    public CheckedTextView checkedText ;
}

public FriendsSimpleCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context = context ;
    this.values = from ;
    this.layout = layout ;
    this.cursor = c ;
    Log.d(TAG, "At the end of the constructor") ;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)   {
    Log.d(TAG, "At the start of rowView. position = " + position) ;
    View rowView = convertView ;
    if(rowView == null) {
        Log.d(TAG, "rowView = null");
        try {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(layout, parent, false);
        Log.d(TAG, "rowView inflated. rowView = " + rowView);
        ViewHolder viewHolder = new ViewHolder() ;
        viewHolder.checkedText = (CheckedTextView) rowView.findViewById(R.id.text1) ;
        rowView.setTag(viewHolder);
        }
        catch (Exception e) {
            Log.e(TAG, "exception = " + e);
        }
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();

    int nameCol = cursor.getColumnIndex(FriendsDbAdapter.KEY_NAME) ;
    String name = cursor.getString(nameCol);
    holder.checkedText.setText(name);

    Log.d(TAG, "At the end of rowView");
    return rowView;

}

}

Granado answered 22/2, 2012 at 17:31 Comment(1)
According to this link, codereview.stackexchange.com/questions/1057/… we are better off overriding newView and bindView.Granado
O
1

What I did that solved it:

cur = cursor. c = the simplecursoradapter inner cursor.

in my MainActivity:

(members)
static ArrayList<Boolean> checkedStates = new ArrayList<Boolean>();
static HashSet<String> selectedIds = new HashSet<String>();
static HashSet<Integer> selectedLines = new HashSet<Integer>();

in the listView onItemClickListener:

if (!selectedIds.contains(bookmarkID)) {

    selectedIds.add(bookmarkID);
    selectedLines.add(position);


} else {

     selectedIds.remove(bookmarkID);
     selectedLines.remove(position);



 if (selectedIds.isEmpty()) {
    //clear everything
        selectedIds.clear();
        checkedStates.clear();      
        selectedLines.clear();

        //refill checkedStates to avoid force close bug - out of bounds
        if (cur.moveToFirst()) {
            while (!cur.isAfterLast()) {    
                MainActivity.checkedStates.add(false);

                cur.moveToNext();
            }
        }                       

 }

In the SimpleCursorAdapter I added (both in getView):

// fill the checkedStates array with amount of bookmarks (prevent OutOfBounds Force close)
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {  
                MainActivity.checkedStates.add(false);
                c.moveToNext();
            }
        }

and:

String bookmarkID = c.getString(0);
        CheckedTextView markedItem = (CheckedTextView) row.findViewById(R.id.btitle);
        if (MainActivity.selectedIds.contains(new String(bookmarkID))) {
            markedItem.setChecked(true);
            MainActivity.selectedLines.add(pos);

        } else {
            markedItem.setChecked(false);
            MainActivity.selectedLines.remove(pos);
        }

Hope this helps... you'll of course need to adjust it to your needs.

EDIT:

Downloaded FB SDK, couldn't get pass the FB login. you have a bug where you don't get a valid access_token if the FB app is installed on the device. Removed the FB app and got a FC in getFriends(). Solved it by wrapping its scope with runOnUiThread(new Runnable...).

The error you get has nothing to do with bad filtering, bad checkbox states... You get it because you're trying to access the cursor before querying it (already closed). It seems like you're closing the cursor before the adapter is using it. Verified it by adding:

mDbHelper = new FriendsDbAdapter(context);

        mDbHelper.open() ;
        cursor = mDbHelper.fetchAllFriends();

to the getView scope in SelectFriendsAdapter.

After you add this, it won't FC and you can start taking care of your filter. Make sure the cursor is not closed and basically if you're managing it with startManagingCursor(), there's not need to manually close it.

Hope you can take it from here!

Oblique answered 22/2, 2012 at 18:48 Comment(7)
Thanks a lot for your help. I am still facing trouble in setFilterQueryProvider. Using the same database helper as the one used for SimpleCursorAdapter is giving me a "Invalid Statement in fillWindow() error" In my listactivity, I have used an instance of mDbHelper which is my DatabaseHelper. I make a call to c = mDbHelper.fetchAllFriends() followed by a startManagingCursor() in the onCreate of my listActivity. Should I declare a new instance of mDbHelper for the call in runQuery()? I am not able to find what is the preferred way to deal with this.Granado
@movingahead I'm not sure I understand how to help you and how your current question is related to what you described in the OP post, BUT, following the error you receive, it seems like the right way is to instantiate the mDbHelper in the onCreate (new mDbHelper() or whatever) and then call the fetchFriends. See here: 1. #4195589 2. #4258993 3. #9050042Oblique
I have followed all the links that you pointed to. I am getting one or another cursor error. Anyways, thanks for the help.Granado
@movingahead too bad it's not solved... I guess I would've been able to help more if I saw your code or at least the part that includes the line the stack trace points as the error.Oblique
My entire code is at github.com/movingahead/GroupBanker/tree/development/src/me/… The relevant files for this error are SelectFriends.java, SelectFriendsAdapter.java and FriendsDbAdapter.java The exact line for error is github.com/movingahead/GroupBanker/commit/… It will be great if you can see if I am making some obvious mistake.Granado
@movingahead See update in my answer (bottom part after "Edit"). Hope this will lead you to the right solution.Oblique
Thanks a lot. I will try implementing this.Granado
R
0

adapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {

                return db.fetchdatabyfilter(constraint.toString(), "name");

                }
        });
Rajkot answered 12/7, 2018 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.