cursorAdapter过滤listview的两个标准

时间:2015-04-06 11:53:43

标签: android android-listview simplecursoradapter

我有一个listview,它由产品标签和Code填充,来自数据库,

我想通过这些“标签”和“代码”值(双重过滤)过滤listview,但我不知道该怎么做。 并且在我输入3个字符之后没有过滤器,所有列表如下所示。

有人帮忙吗?

   public class MainActivity extends Activity implements OnItemClickListener {

    private ListView lv;
    EditText searchBox;

   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Database db = Database.getDatabase();
    lv = (ListView) findViewById(R.id.list_view);
    searchBox = (EditText) findViewById(R.id.edSearch);

   lv.setVisibility(View.GONE);

   displayListView();
   }

  private void displayListView() {

    Cursor cursor = myDb.getDatabaseEntityOperations().fetchEntities(
            searchBox);


    String[] columns = new String[] 
       {
            myDb.getDatabaseEntityOperations().entity_Code,
            myDb.getDatabaseEntityOperations().entity_Label };

    // the XML defined views which the data will be bound to
    int[] boundTo = new int[] { R.id.product_code, R.id.product_label };

    // create the adapter using the cursor pointing to the desired data
    // as well as the layout information

            cursorAdapter = new SimpleCursorAdapter(this,
            R.layout.single_search_item, cursor, columns,boundTo,0);

    // Assign adapter to ListView
           lv.setAdapter(cursorAdapter);

           lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view,
                int position, long id) {
            // Get the cursor, positioned to the corresponding row in the
            // result set

            Cursor cursor=cursorAdapter.getCursor();

            String pass_data_label = cursor.getString(4).toString();
            String pass_data_code= cursor.getString(3).toString();


            Intent intent = (new     Intent(MainActivity.this,ProductInfo.class));              
            intent.putExtra("label",pass_data_label);
            intent.putExtra("code",pass_data_code);

            startActivity(intent);
            // Get the state's capital from this row in the database.

        }
    });

    searchBox.addTextChangedListener(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) {
            // TODO Auto-generated method stub
            cursorAdapter.notifyDataSetChanged();
            cursorAdapter.getFilter().filter(s.toString());
            if (searchBox.length() >= 3) {
                lv.setVisibility(View.VISIBLE);
            } else if (searchBox.length() < 3) {
                lv.setVisibility(View.GONE);

            }
        }

    });

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

            return myDb.getDatabaseEntityOperations().fetchEntities(
                    searchBox);
        }
    });

}

0 个答案:

没有答案