使用SimpleCursorAdapter使用EditText搜索Listview

时间:2013-09-18 20:04:00

标签: android listview search android-listview simplecursoradapter

我正在尝试搜索我通过SimpleCursorAdapter创建的ListView。我已经尝试了各种各样的方法,以下是我迄今为止所做的并且它不起作用。我希望能够输入EditText,我的列表会调整到搜索范围。任何帮助将不胜感激,下面是我的代码:

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

    });
}

我可以输入文字,但没有任何反应。 干杯

1 个答案:

答案 0 :(得分:0)

试试这个

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

在我看来,你需要一个FilterQueryProvider:)

myCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {

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

    }
});

检查一下 - Using an EditText to filter a SimpleCursorAdapter-backed ListView