java 如何在微调器中设置 ID 和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29717003/
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-11-02 15:46:57  来源:igfitidea点击:

How to Set ID And Value in spinner

javaandroidandroid-activity

提问by Jaipal Singh Dungarpur

I created a Data-Base table student which contains two fields like student_id and student_name. This table contains some data for student.

我创建了一个数据库表学生,其中包含两个字段,如 student_id 和 student_name。此表包含学生的一些数据。

My actual problem is I want to set ID and name of student in spinner like in html select tag, so when select spinner item I must get this ID to get another data from database.

My actual problem is I want to set ID and name of student in spinner like in html select tag, so when select spinner item I must get this ID to get another data from database.

回答by Android Killer

You can have two arraylist for name and id;

你可以有两个用于 name 和 id 的数组列表;

List<String> sIds = new ArrayList<String>();//add ids in this list
List<String> names = new ArrayList<String>();//add names in this list

Then you can retrive here inside spinner select. It will work even if two names are same in the spinner which may be the case:

然后你可以在微调器选择中检索这里。即使微调器中的两个名称相同,它也会工作,可能是这种情况:

  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            int id = sIds.get(position);//This will be the student id.
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

回答by Tristan Wiley

Assuming I am reading your list correctly, if you want to set an Id to your Spinner programically just use..

假设我正确阅读了您的列表,如果您想以编程方式为 Spinner 设置 Id,只需使用..

mSpinner.setId(int)

In this code replace mSpinner with the reference to your Spinner and int with the desired Id

在此代码中,将 mSpinner 替换为对 Spinner 的引用,并将 int 替换为所需的 Id

回答by Skizo-oz??S

To add values into Spinner you can do it something like this Test

要将值添加到 Spinner,您可以执行以下操作: Test

 public class Test extends Activity 
{


 ArrayList<String> InfoStudent = new ArrayList<String>();
 ArrayList<String> IdStudent = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

  Spinner spinner = (Spinner) findViewById(R.id.spinner1); 


   //Here you'll have to make a query to get ID and Name then with 
  InfoStudent.add("TheID" + "InfoStudentReturnedByQuery");
  //TheID should go in IdStudent ArrayAdapter and InfoStudentReturnedByQuery should go in InfoStudent ArrayAdapter

  // Then you create the arrayAdapter
  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Test.this
            ,android.R.layout.simple_spinner_item,difficultyLevelList);

                 // Set the Adapter
  spinner.setAdapter(arrayAdapter);

If you want to know what spinner is selected you should do something like this :

如果您想知道选择了哪个微调器,您应该执行以下操作:

          spinner.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

              public void onItemSelected(AdapterView<?> adapterView, 
             View view, int i, long l) { 
             // TODO Auto-generated method stub
          Toast.makeText(MainActivity.this,"You Selected : "
           + TheMainAdapterOfStudents.get(i)+" Student ",Toast.LENGTH_SHORT).show();

               }

This only it's a guide how to, hope it helps :)

这只是一个指南,希望它有帮助:)

回答by Jeka Developer

1. Declare type Like:

1.声明类型喜欢:

public class Item
{
public long id;
public String name;
}

2. Fill spinner with data (Item[]):

2. 用数据填充微调器 (Item[]):

ArrayAdapter<DictionaryEntity> dataAdapter = new ArrayAdapter<DictionaryEntity>this,android.R.layout.simple_spinner_item, data);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

3. Get your id where you need it:

3. 在需要的地方获取您的 ID:

Item item = (Item)spinner.getSelectedItem();
return item.id;