过滤ListView SimpleCursorAdapter setFilterQueryProvider

时间:2015-04-21 22:16:58

标签: android sqlite listview filter

我想要使用多个" Like?"过滤ListView中项目的Atomic_Number和名称。没有成功。我的错是什么?

private ListView lvElementos;
private EditText etBuscar;
private SimpleCursorAdapter adaptador;
private Cursor cur_Elements;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    lvElementos = (ListView) findViewById(R.id.LV_Elementos);

    lvElementos.setFastScrollEnabled(true);
    lvElementos.setTextFilterEnabled(true);

    //ListView SQlite- Open Database
    DatabaseHelper admin_Elements = new DatabaseHelper(this);
    final SQLiteDatabase bd_Elements = admin_Elements.getWritableDatabase();
    cur_Elements = bd_Elements.rawQuery("select _id, (Atomic_Number||'. '||Name)as Name from DataTP", null);

    if (cur_Elements.moveToFirst()) {
        String[] from = new String[]{"Name"};
        int[] to = new int[]{android.R.id.text1};
        adaptador = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur_Elements, from, to, 0);
        lvElementos.setAdapter(adaptador);
    }
 else {
            Toast.makeText(this, "Error retrieving element's name database", Toast.LENGTH_SHORT).show();
        }
        //cur_Elements.close();

    lvElementos.setOnItemClickListener(this);

    etBuscar = (EditText) findViewById(R.id.et_buscar);

    etBuscar.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

            MainActivity.this.adaptador.getFilter().filter(cs.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence cs, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable cs) {
            // TODO Auto-generated method stub

        }
    });

    //Check This
    adaptador.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {


            final String[] Columns={"_id","(Atomic_Number||'. '||Name)as Name"};
            return bd_Elements.query("DataTP",Columns,"Name like ?",new String[] { constraint + "%" },null,null,null,null);//Atomic_Number not filter

//return bd_Elements.query("DataTP",Columns,"Atomic_Number like ? OR Name like ?",new String[] { constraint + "%" },null,null,null,null);//Name not filter
        }
    });

}

我在setFilterQueryProvider中做了很多变种,但没有变种。在EditText中,我尝试过滤只显示两列中的一列而另一列消失。

0 个答案:

没有答案