我是Android新手编程的新手,我正在尝试创建一个应用程序,用户可以从2个微调器中选择一些选项,点击搜索按钮后,应用程序将根据所选的选项搜索数据库结果会显示出来。问题是我不确定要使用所选微调器选项的值进行db.rawQuery搜索的参数。我试着在下面粗略猜测,但它不起作用,我不知道如何使用这两个标准进行搜索。
以下是微调器的编码:
public class First extends Activity {
private Spinner foodSpinner, locationSpinner;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
addListenerOnButton();
}
public void addListenerOnButton() {
foodSpinner = (Spinner) findViewById(R.id.foodSpinner);
locationSpinner = (Spinner) findViewById(R.id.locationSpinner);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(First.this.getApplicationContext(), MainActivity.class);
intent.putExtra("FOODSPINNER", foodSpinner.getSelectedItem().toString());
intent.putExtra("LOCATIONSPINNER", locationSpinner.getSelectedItem().toString());
First.this.startActivity(intent);
}
});
}
以下是搜索的编码:
public class MainActivity extends ListActivity {
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
protected String foodItemChosen;
protected String locationItemChosen;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
foodItemChosen = getIntent().getExtras().getString("FOODSPINNER");
locationItemChosen = extras.getString("LOCATIONSPINNER");
db = (new DatabaseHelper(this)).getWritableDatabase();
}
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, ResultDetails.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("FOOD_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
public void search(View view) {
cursor = db.rawQuery("SELECT _id, name, address FROM database WHERE food LIKE ?",
new String[]{"%" '+foodItemChosen+' "%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.result_list_item,
cursor,
new String[] {"name", "address"},
new int[] {R.id.name, R.id.address});
setListAdapter(adapter);
}
}
答案 0 :(得分:0)
然后最好使用query
方法而不是rawQuery
。它提供了从数据库中选择项目的灵活方式。
一些有用的文章
http://www.vogella.com/articles/AndroidSQLite/article.html
http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android