我正在制作食谱应用程序。我通过食谱部分进行搜索工作正常
public class MainActivity extends Activity {
protected ListView lv;
protected ListAdapter adapter;
SQLiteDatabase db;
Cursor cursor;
EditText et_db;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DB_Recipe(this)).getWritableDatabase();
lv = (ListView) findViewById(R.id.lv);
et_db = (EditText) findViewById(R.id.et);
try {
cursor = db.rawQuery("SELECT * FROM recipe ORDER BY name ASC", null);
adapter = new SimpleCursorAdapter(this, R.layout.isi_lv, cursor,
new String[] { "name", "ingredients", "img" }, new int[] {
R.id.tv_name, R.id.tvIngredients, R.id.imV });
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
detail(position);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public void search_db(View v) {
String edit_db = et_db.getText().toString();
if (!edit_db.equals("")) {
try {
cursor = db.rawQuery("SELECT * FROM recipe WHERE name LIKE ?",
new String[] { "%" + edit_db + "%" });
adapter = new SimpleCursorAdapter(
this,
R.layout.isi_lv,
cursor,
new String[] { "name", "ingredients", "img" },
new int[] { R.id.tv_name, R.id.tvIngredients, R.id.imV });
if (adapter.getCount() == 0) {
Toast.makeText(
this,
"Recipe not found " + edit_db
+ "", Toast.LENGTH_SHORT).show();
} else {
lv.setAdapter(adapter);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
cursor = db.rawQuery("SELECT * FROM recipe ORDER BY name ASC",
null);
adapter = new SimpleCursorAdapter(
this,
R.layout.isi_lv,
cursor,
new String[] { "name", "ingredients", "img" },
new int[] { R.id.tv_name, R.id.tvIngredients, R.id.imV });
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void detail(int position) {
int im = 0;
String _id = "";
String name = "";
String ingredients = "";
String preparation = "";
if (cursor.moveToFirst()) {
cursor.moveToPosition(position);
im = cursor.getInt(cursor.getColumnIndex("img"));
name = cursor.getString(cursor.getColumnIndex("name"));
ingredients = cursor.getString(cursor.getColumnIndex("ingredients"));
preparation = cursor.getString(cursor.getColumnIndex("preparation"));
}
Intent iIntent = new Intent(this, DB_Parse.class);
iIntent.putExtra("dataIM", im);
iIntent.putExtra("dataName", name);
iIntent.putExtra("dataIngredients", ingredients);
iIntent.putExtra("dataPreparation", preparation);
setResult(RESULT_OK, iIntent);
startActivityForResult(iIntent, 99);
}
}
无论如何,我可以改变它,以便您可以按成分而不是食谱进行搜索,或者我将如何进行搜索
答案 0 :(得分:0)
如果您只想按成分搜索,可以更改public void search_db(View v)
按照食谱名称搜索的查询:
cursor = db.rawQuery("SELECT * FROM recipe WHERE name LIKE ?",
new String[] { "%" + edit_db + "%" });
为:
cursor = db.rawQuery("SELECT * FROM recipe WHERE ingredients LIKE ?",
new String[] { "%" + edit_db + "%" });