samedi 1 septembre 2018

SearchView suggestions adapter + checkbox

Background: I am trying to show a drop-down of suggestions when the user searches for a keyword in the searchview using the suggestions adapter.

I am able to display the drop-down fine with all the elements. My concern lies with the checkbox selection. For example, when a user enters "a" in the searchview, the user would see "apple", "avocado" etc. in the dropdown with the checkboxes.

Issue: User searched for Avocado so Avocado is now item 0 in the drop-down,

User checked Avocado which sets position 0 as checked,

We redisplay the whole list.

Now Apple is position 0 so it shows as checked even though the user never selected Apple in the first place.

How do I maintain the correct order of checkbox selected?

What I have tried: I know that we can use a unique ID via the getItemId to identify the row that was selected instead of using the checkbox position. I am however confused as to how to use it in my CursorAdapter. Have been stuck on this problem for quite some time now and would really appreciate some help.

Most of the samples online don't use a suggestionsAdapter. However, I need to make this work via a suggestionsAdapter only which in turns uses a CursorAdapter.

Code snippet:

public class SearchViewSuggestionsAdapter extends CursorAdapter implements AdapterView.OnItemClickListener{


    TextView tagName;

    CheckBox checkBox;

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private Gson gson = new Gson();
    private LongSparseArray<Boolean> mCheckedState;

    private SearchViewSuggestionsAdapterInterface searchViewSuggestionsAdapterInterface;


    public SearchViewSuggestionsAdapter(Context context,
                                        Cursor c,
                                        boolean autoRequery,
                                        SearchViewSuggestionsAdapterInterface searchViewSuggestionsAdapterInterface) {
        super(context, c, autoRequery);
        this.searchViewSuggestionsAdapterInterface = searchViewSuggestionsAdapterInterface;
        pref = context.getSharedPreferences("Tags", 0);
        editor = pref.edit();
        mCheckedState = new LongSparseArray<>();
    }


    @Override
    public long getItemId(int position) {
        Cursor cursor = getCursor();
        cursor.moveToPosition(position);
        return cursor.getLong(mCursor.getColumnIndex("_id"));
    }

    @Override
    public View newView(Context context, final Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;
        if (cursor.getString(cursor.getColumnIndex("type")).equals("header")) {
            view = inflater.inflate(R.layout.search_header, parent, false);
        } else if (cursor.getString(cursor.getColumnIndex("type")).equals("tags")) {
            view = inflater.inflate(R.layout.search_tags, parent, false);
            final ViewHolder viewHolder = new ViewHolder();

            viewHolder.checkBox = view.findViewById(R.id.fab);
            viewHolder.tagName = view.findViewById(R.id.tag);

            viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


/*
WHAT SHOULD I DO HERE?

**/
            view.setTag(viewHolder);
        }


 @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        final ViewHolder viewHolder = (ViewHolder) view.getTag();

        if (cursor.getString(cursor.getColumnIndex("type")).equals("tags")) {

            viewHolder.tagName.setText((cursor.getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_2))));
        viewHolder.checkBox.setTag(cursor.getInt(cursor.getColumnIndex("_id")));
           // viewHolder.checkBox.setTag(cursor.getPosition());
/*
AM I MISSING SOMETHING HERE?

**/

        }


    }




Aucun commentaire:

Enregistrer un commentaire