2011-12-13 45 views
-2

正如你可以看到這是我的列表類。我想能夠使用editText過濾此列表。任何人都可以在正確的方向上指向我所需的查詢佈局或甚至代碼片段?如何過濾數據庫內容以填充ListView?

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


} 

回答

0

我不是偉大的SQLite的,但我認爲,如果你只是把你的過濾器 查詢它應該只吐回你想要的東西。

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

然後,只需調整這個你需要覆蓋的所有表。

+0

對不起,這個光標與我的代碼相關? – aspiringCoder

+0

你是什麼意思?我從哪裏得到[光標]?你把這個方法放在哪裏? – AedonEtLIRA

+0

是的方法存儲在哪裏?在適配器類中?數據庫幫助類還是這個? – aspiringCoder

相關問題