如何在 Android Spinner 中设置数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11668523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:25:14  来源:igfitidea点击:

How do I set data in an Android spinner?

androidandroid-spinner

提问by user1551503

I was successful in getting a value from the spinner and storing it in a databse, but at return, I couldn't set Textinto Spinner. How do I do this like the other function of EditText?

我成功地从旋转得到一个值,并将其存储在DATABSE,但回报,我不能设置TextSpinner。我如何像 的其他功能一样做到这一点EditText

This is my code:

这是我的代码:

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);     
              }
          });
    }
}

I saved the second item into my database from my spinner. Now my spinner is reset. Then, I click on the Show button. The item I saved will show in the spinner as data. Why is this failing?

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

回答by Hiral Vadodaria

If i didn't get you wrong,you are trying to find the way you can set a text as selected item in spinner,which is one of the items from your database you have list of,in spinner.

如果我没有误会您,您正在尝试找到可以在微调器中将文本设置为选定项目的方法,这是您在微调器中拥有的数据库列表中的项目之一。

If so,then this might help you:

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

Example:

例子:

  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
    }

This will be like:

这将是这样的:

You have these options in database:

您在数据库中有这些选项:

1.apple

1.苹果

2.orange

2.橙色

3.pineapple

3.菠萝

4.strawberry

4.草莓

5.grapes

5.葡萄

now,you want to show pineapple to be selected,then,

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

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

this line will set it accordinly.

这一行将相应地设置它。

EDIT :Try with this:

编辑:试试这个:

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)));     
              }
          });
    }
}