我有一个我从数据库中填充的微调器。我想选择默认选择列表中的哪个项目。我需要找出列表中的哪个项目(CursorAdapter)具有值“Default Away”并将其设置为所选值。
Spinner away_team_spinner = (Spinner)findViewById(R.id.away_team_spinner);
DatabaseHelper db = new DatabaseHelper(this);
Cursor team_list = db.getTeams(p_game_level);
startManagingCursor(team_list);
String[] team_name = new String[]{colTeamName};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, team_list, team_name, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
away_team_spinner.setAdapter(adapter);
//// HERE IS WHERE MY ERRORS START ////
Log.i("NEW_GAME","Before set arrayadapter");
CursorAdapter adapter_choose = (CursorAdapter)away_team_spinner.getAdapter();
Log.i("NEW_GAME","Before set setSelection");
away_team_spinner.setSelection(adapter_choose.getPosition("Default Away"));
这是我通过在这个网站上搜索找到的“解决方案”。但是,我不能将“getPosition”与CursorAdapter对象一起使用。我尝试了ArrayAdapter,但是在“之前设置arrayadapter”注释错误之后的行“android.widget.SimpleCursorAdapter无法转换为android.widget.ArrayAdapter”。我究竟做错了什么?感谢。
答案 0 :(得分:8)
您是否考虑过运行for循环,直到找到位置然后设置适配器位置?起草一些代码,然后测试它,我正在做类似的事情
而且这只是诀窍,享受!
int cpos = 0;
for(int i = 0; i < simpleCursorAdapter.getCount(); i++){
cursor.moveToPosition(i);
String temp = cursor.getString((your column index, an int));
if ( temp.contentEquals(yourString)){
Log.d("TAG", "Found match");
cpos = i;
break;
}
}
spinner.setSelection(cpos);