我有一个用sqlite中的值(类别表中的类别)填充的微调器,现在我需要一个select查询来将项目检索到listView中。使得在微调器选择上改变列表视图值也改变。items来自另一个表(items)。cid是items表中的外键,cid是category表中的主键。。。。
String select = "SELECT " +
Support.KEY_INAME + " FROM "
+ Support.TABLE_ITEMS + " LEFT JOIN "
+ Support.TABLE_CAT + " ON "
+ (Support.TABLE_ITEMS + "." + Support.KEY_CID) +" = "
+ (Support.TABLE_CAT + "." + Support.KEY_CID)
+ " WHERE " + Support.TABLE_CAT + "." + Support.KEY_CID + " =?";
这是我的微调器选择和加载listview数据代码。。。
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
Spinner spinner = (Spinner)parent;
if (spinner.getId() == R.id.spinner1) {
dbRepo = new DBRepo(getApplicationContext());
final List<Support> list = dbRepo.getItems1();
adapter = new Custom(Category.this, R.layout.view_entry, list);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
adapter.notifyDataSetChanged();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
上面的查询是真的,或者我使用这两个查询来解决我的问题
String selectQuery = "SELECT * FROM " + TABLE_ITEM + " WHERE " + KEY_CID + " ='" + cid + "'";
Cursor c = db.rawQuery(selectQuery, null);
或
Cursor c = db.rawQuery("SELECT * FROM item_table WHERE _CategoryID =? " ,new String[]{cid}, null );
和change也更改代码微调器。setonItemSelected像这个
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
handler = new DBSQLite(getBaseContext());
String selected = (String) parent.getItemAtPosition(position);
// Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
String cid = handler.getCid(selected);
final ArrayList<Support> item = handler.getItems(cid);
adapter = new CustomAdapter(AvailableItems.this, R.layout.available_view, item);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});