如何过滤数据库内容以填充ListView?

时间:2011-12-13 15:20:31

标签: android listview filter

正如您所看到的,这是我的列表类。我希望能够使用editText过滤此列表。任何人都可以指出我所需的查询布局或代码片段的正确方向吗?

public class CBFilter extends ListActivity {

EditText Filter;
ListView RecipeNames;
Cursor cursor;
CBListAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText, RowId;
String[] from = { CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.rowText};
ImageView image;
byte[] dataImage;
BufferedInputStream buf;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean diditwork;
try{

//FileInputStream in;//= openFileInput("//STEFAN-PC/Users/stefan/Desktop/Uni Work/Image.jpg");
RecipeNames = (ListView) findViewById(android.R.id.list);
RecipeNames.setTextFilterEnabled(true);
RecipeText = (TextView) findViewById(R.id.recipeText);
Filter = (EditText) findViewById(R.id.search_box);

//adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
image = (ImageView) findViewById(R.id.RecipeImage);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
adapter = new CBListAdapter(this, 0, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();

Filter.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

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

    public void afterTextChanged(Editable s) {

        RecipeNames = (ListView) findViewById(android.R.id.list);
        CBListAdapter filteradapter = (CBListAdapter)RecipeNames.getAdapter();
        filteradapter.getFilter().filter(s.toString());


    }




});

 /*adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return getDirectoryList(constraint);
        }
    });*/


/*adapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
        return getDirectoryList(constraint);
    }
});*/


}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("darn");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}}


/*public Cursor getDirectoryList (CharSequence constraint)  {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(
        CBDataBaseHelper.DATABASE_TABLE
    );

    String asColumnsToReturn[] = { 
            CBDataBaseHelper.DATABASE_TABLE + "."
            + CBDataBaseHelper.KEY_NAME 
    };

    if (constraint == null  ||  constraint.length () == 0)  {
        //  Return the full list
        return queryBuilder.filter(, asColumnsToReturn, null, null,
                null, null, null);  }  
    else  {
        String value = "%"+constraint.toString()+"%";

        return data.query(CBDataBaseHelper.DATABASE_TABLE, asColumnsToReturn, "LAST_NAME like ? ", new String[]{value}, null, null, null);
    }
}*/

public void CreateNew(View view){

    Intent myIntent = new Intent(this, CBCreate.class);
    startActivity(myIntent);
}

@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
    super.onListItemClick(parent, v, position, id);
    Intent intent1 = new Intent(this, CBCreate.class);
    long rowId = cursor.getLong(cursor.getColumnIndex(CBDataBaseHelper.KEY_ROWID));
    String s = String.valueOf(rowId);
    intent1.putExtra("SELECTED", s);
    startActivity(intent1);
}


}

1 个答案:

答案 0 :(得分:0)

我在SQLite上并不擅长,但我想如果你只是放置你的过滤器 在查询中它应该只吐出你想要的东西。

public Cursor findRecipe(String filter) {
    return db.query("myrecipetable", null, 
        "rec_recipecolumn LIKE" + filter, null, null, null, null);
}

然后只需针对您需要涵盖的所有表格进行调整。