如何在Android微调器中设置数据?

时间:2012-07-26 11:42:53

标签: android android-spinner

我成功地从微调器获取值并将其存储在数据库中,但是在返回时,我无法将Text设置为Spinner。我如何像EditText的其他功能一样执行此操作?

这是我的代码:

public class MainActivity_spinner extends Activity
{
     Button save;
     Button show;
     public void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          String[] items = new String[] {" ","Male","Female"};
          final Spinner gender =(Spinner)findViewById(R.id.sex);
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          gender.setAdapter(adapter);
          save=(Button)findViewById(R.id.save);
          show=(Button)findViewById(R.id.show);
           //----------------------------------------------------------------    
          save.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View view){
                   ContentValues values = new ContentValues();
                   values.put("sex",gender.getSelectedItem().toString());
                   sql.Insert("db",null,values);
                   sql.Close();
              }
          });
          show.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View view){
                   sql.open();
                   String query = "select sex from db where id=2";
                   Cursor c = sql.rawQuery(query,null);
                   c.moveToFirst();
                   (Error is here)it is my spinner i want show value >>>>>>          gender.setSelection(c.getString(0), true);     
              }
          });
    }
}

我从我的微调器中将第二个项目保存到我的数据库中。现在我的微调器被重置了。然后,我单击“显示”按钮。我保存的项目将在微调器中显示为数据。为什么这会失败?

1 个答案:

答案 0 :(得分:6)

如果我没有弄错你,你试图找到你可以在spinner中将文本设置为选定项目的方式,这是你在旋转器中列出的数据库中的项目之一。

如果是这样,那么这可能会对您有所帮助:

示例:

  ArrayList<String> options=new ArrayList<String>();
    ...onCreate(){
      ...
      Cursor c=<your cursor with value>;
      for(loop for total records){
           options.add(<value from cursor>);
      }
      //now you have options with all values of your database

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,options);
      mSpinner.setAdapter(adapter); // this will set list of values to spinner

      mSpinner.setSelection(options.indexOf(<value you want to show selected>)));//set selected value in spinner
    }

这就像:

您在数据库中有以下选项:

1.apple

2.orange

3.pineapple

4.strawberry

5.grapes

现在,您要显示要选择的菠萝,

mSpinner.setSelection(options.indexOf("pineapple")));

这条线将按顺序设置。

编辑: 试试这个:

public class MainActivity_spinner extends Activity
{
     Button save;
     Button show;
     ArrayList<String> items=new ArrayList<String>();
     Spinner gender;

     public void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          items.add("Male");
          items.add("Female");

          gender =(Spinner)findViewById(R.id.sex);
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);              
          gender.setAdapter(adapter);

          save=(Button)findViewById(R.id.save);
          show=(Button)findViewById(R.id.show);
           //----------------------------------------------------------------    
          save.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View view){
                   ContentValues values = new ContentValues();
                   values.put("sex",gender.getSelectedItem().toString());
                   sql.Insert("db",null,values);
                   sql.Close();
              }
          });
          show.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View view){
                   sql.open();
                   String query = "select sex from db where id=2";
                   Cursor c = sql.rawQuery(query,null);
                   c.moveToFirst();
                   gender.setSelection(items.indexOf(c.getString(0)));     
              }
          });
    }
}