我遇到了遊標和SimpleCursorAdapter的問題。SimpleCursorAdapter將不會工作,因爲缺少_id
我想要完成的任務:
我有3場
數據庫_id |暱稱| somethingelse
我想明確地選擇所有的nickNames並在AutoCompleteTextView中提供它們。
問題:
當進行查詢(見下文),我沒有選擇_id場。這就是爲什麼當我嘗試創建SimpleCursorAdapter時出現錯誤,因爲在遊標中沒有字段「_id」。 但是,如果我在查詢中選擇「_id」,遊標中的nickNames將不再是destinct了!另外一個光標是不可修改的,據我所知還是它?
所以我想出了一個可行的解決方法,但是編程風格相當糟糕,因爲我在做兩倍的工作......我只是將相同的數據放入另一個容器然後使用它。這沒有直接的辦法嗎?這實際上是一個簡單的任務...必須有一種方法來做到這一點,我不明白這一點。
下面的代碼:
protected void onPrepareDialog(int id, final Dialog dialog)
{
switch(id)
{
case R.layout.database_feed:
/*get all distinct names in the Database */
Cursor cursor2 = mDatabase.rawQuery("SELECT DISTINCT nickName FROM highscore_table "+
"ORDER BY nickName COLLATE NOCASE", null);
/* I didn't find a simple way to set cursor2 in a
* CursorAdapter for the AutoCompleteTextView.
* this was my first try (does not work): */
/*((AutoCompleteTextView) dialog.findViewById(R.id.actvName)).setAdapter(
new SimpleCursorAdapter(this,android.R.layout.simple_dropdown_item_1line,
cursor2, new String[] { "nickName" },
new int[] { android.R.layout.simple_dropdown_item_1line })
); */
/*this is my workaround ... it works but it's horrible*/
LinkedList<String> llsNames = new LinkedList<String>();
for(cursor2.moveToFirst(); !cursor2.isAfterLast(); cursor2.moveToNext())
{
llsNames.addLast(cursor2.getString(0));
}
((AutoCompleteTextView) dialog.findViewById(R.id.actvName)).setAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_dropdown_item_1line, llsNames
));
break;
default:
break;
}
}
感謝所有幫助
謝謝你,這實際上解決了_id問題。但是現在我注意到我必須實現AutoCompleteTextView的過濾功能,因爲SimpleCursorAdapter不能自行完成。所以我認爲我會堅持解決方案解決方案。但謝謝你的提示!順便說一下:這是一種更改光標顯示的方法:[Google Groups](http://groups.google.com/group/android-developers/browse_thread/thread/796342d2a30c0c0b?pli=1) – Michi 2012-03-22 08:42:33