_id
Androidスピナー/ SimpleCursorAdapterの悪用を検討しています:
// get a cursor from the database with an "_id" field
Cursor c = db.rawQuery("SELECT id, key AS _id, desc FROM type_enum WHERE active != 0 AND cust_id = 1", null);
// make an adapter from the cursor
String[] from = new String[] {"desc"};
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
// set layout for activated adapter
sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// get xml file spinner and set adapter
mTypeView.setAdapter(sca);
// set spinner listener to display the selected item key
mContext = this;
mTypeView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long key){
Toast.makeText(mContext, "Selected key=" + key, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {}
});
これは悪い考えですか?
key
2 番目のクエリを実行せずに、必要なものを取得するより良い方法はありますか?
(テーブルには一意のid
フィールドがあり、句key
の制約が与えられた場合にのみ一意です。)where