Question

I am trying to implement a search of a ListView I create through SimpleCursorAdapter. I have tried various ways and below is what I have so far and it is not working. I want to be able to type in the EditText and my list adjust to the search. Any help would be greatly appreciated and below is my code:

    private FoodDatabase foodDB;
private CalendarDBAdapter calendarDB;
private ListView fruitsListVIew;
private String datePassed;
private View dialogView;
private EditText search;
private SimpleCursorAdapter myCursorAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fruits);        
    fruitsListVIew = (ListView)this.findViewById(R.id.fruitsListView);
    search = (EditText)this.findViewById(R.id.search_box);
    datePassed = GlobalVars.getDate();

    openDB();
    populateLV();

    search.addTextChangedListener(new TextWatcher() 
    {           
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) 
        {
            myCursorAdapter.getFilter().filter(s.toString());
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) 
        {

        }
    });
}

    private void populateLV()
{
    String fruitID = "10";
    final Cursor c = foodDB.getFoodByGroup(fruitID);

    //allow activity to close cursor
    startManagingCursor(c);

    //Mapping from cursor to view fields
    String[] fieldNames = new String[] {FoodDatabase.FOOD_ITEM_NAME, FoodDatabase.CALORIES, FoodDatabase.FAT,
                                        FoodDatabase.MEASURE, FoodDatabase.TYPE_OF_MEASRE};
    int[] viewIds = new int[] {R.id.foodNameTxt, R.id.calorieDB, R.id.fatDB, R.id.servingDB, R.id.measureTxt};

    //create adapter to map columns to view exercise UI
    myCursorAdapter = new SimpleCursorAdapter(this, R.layout.foods_data_layout, c, fieldNames, viewIds);

    //Set the adapter for the list view
    fruitsListVIew.setAdapter(myCursorAdapter);
    fruitsListVIew.setTextFilterEnabled(true);
    fruitsListVIew.setOnItemClickListener(new OnItemClickListener() 
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        {
            qtyDialog(v);
        }

    });
}

I am able to input text but nothing happens. Cheers

Was it helpful?

Solution

Try this

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
        myCursorAdapter.getFilter().filter(s.toString());
        myCursorAdapter.notifyDataSetChanged();
    }

It appears to me that you need a FilterQueryProvider :)

myCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {

    @Override
    public Cursor runQuery(CharSequence constraint) {
        String partialValue = constraint.toString();
        return itemData.getAllSuggestedValues(partialValue);

    }
});

Check this - Using an EditText to filter a SimpleCursorAdapter-backed ListView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top