简体   繁体   English

使用Android Java微调器

[英]using android java spinner

i'm trying to use a few spinners in android to display a list of choices to the user. 我正在尝试使用android中的一些微调框向用户显示选择列表。 The spinners are made in xml but i want to use it from java because depending on the user's choice the text i want displayed in spinner will be different. 微调框是在xml中制成的,但我想从Java中使用它,因为根据用户的选择,我想在微调框中显示的文本会有所不同。 The data is stored in a standard String array. 数据存储在标准String数组中。 I've tried many different methods and examples but i couldnt get it to work. 我已经尝试了许多不同的方法和示例,但是我无法使其正常工作。 The Spinners are assigned in java in a loop. 微调器在java中循环分配。

     units[n] = (Spinner) findViewById (data.Eid[n*3+3]);
 ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.unit, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

     units[n].setAdapter(adapter);

i keep getting an error at R.array.unit. 我在R.array.unit上一直收到错误消息。 i tried eclipse suggested suggestion but it didnt work. 我尝试了日食建议,但没有成功。 Is there a better way to do this? 有一个更好的方法吗? I also cant figure out how to add the data from the array to the spinner and i'd like it to have the first option as the default. 我也想不出如何将数据从数组添加到微调器,我希望它具有第一个选项作为默认值。

I believe R.array.unit should be defined in your project because it is not part of android.R.array class. 我相信R.array.unit应该在您的项目中定义,因为它不是android.R.array类的一部分。

For creating a spinner from an array adapter a good example is provided in the developer guide. 为了从阵列适配器创建微调器,开发人员指南中提供了一个很好的示例。 See the next link. 请参阅下一个链接。 http://developer.android.com/resources/tutorials/views/hello-spinner.html http://developer.android.com/resources/tutorials/views/hello-spinner.html

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

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