简体   繁体   English

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

[英]How to Set ID And Value in spinner

I created a Data-Base table student which contains two fields like student_id and student_name. 我创建了一个数据库表student,其中包含两个字段,如student_id和student_name。 This table contains some data for student. 该表包含一些学生数据。

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. 我的实际问题是我想像在html select标签中一样在Spinner中设置学生的ID和名称,因此在选择Spinner项目时,我必须获取此ID才能从数据库中获取其他数据。

You can have two arraylist for name and id; 您可以使用两个arraylist作为名称和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
        }

    });

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

To add values into Spinner you can do it something like this Test 要将数值添加到Spinner中,您可以执行此测试

 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 :) 这只是它的指南,希望对您有所帮助:)

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM