我想在Spinner中显示带有一些标题的所选项目,喜欢的项目是:item6

时间:2016-06-29 10:47:10

标签: android

我是android的新手,我希望工作如下,在Spinner中如果我选择任何其他项目形式数组,该项目应该被选中包括一些文本,例如,如果我的微调器有10个项目,如果我选择第6项该项目应显示为“您的项目是:项目6”,这应该在我的微调器中显示为第一项,在选择项目组成阵列后,请帮助我,提前致谢..

2 个答案:

答案 0 :(得分:0)

String[] categories = {"Select one","Automobile","Computers","Education"};

  // Creating adapter for spinner
  ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

  // attaching data adapter to spinner
  spinner.setAdapter(dataAdapter);

然后onItemSelected Listerner Do: -

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  // On selecting a spinner item
  String item = parent.getItemAtPosition(position).toString();
  categories[0] = item;
  ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
  spinner.setAdapter(newdataAdapter);

  }
public void onNothingSelected(AdapterView<?> arg0) {
  // TODO Auto-generated method stub
  }

答案 1 :(得分:0)

ArrayAdapter arrayAdapter = null;
String[] list=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //you can replace the below list with your list
    list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
    final Spinner spinner = (Spinner) findViewById(R.id.spinner);

    arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(arrayAdapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            spinner.setPrompt("Your selected item is:"+list[position]);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}