2

我有一个从数据库中填充的微调器。我想选择默认选择列表中的哪个项目。我需要找出列表(CursorAdapter)中的哪个项目具有“默认离开”值并将其设置为所选值。

    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”。我究竟做错了什么?谢谢。

4

1 回答 1

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);
于 2012-08-19T05:07:36.640 回答