0

我正在考虑滥用_idAndroid 微调器 / 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我需要的东西,而无需运行第二个查询?

(该表确实有一个唯一id字段,key仅在给定where子句约束的情况下才是唯一的。)

4

0 回答 0