简体   繁体   English

从xml显示项目 <array-list> 根据用户选择

[英]Displaying item from xml <array-list> according to user selection

So lets say I have spinner, from which I choose items, that are stored in string-array, and the user can choose from: 因此,可以说我有一个微调器,从中可以选择存储在字符串数组中的项,用户可以从以下项中进行选择:

<string-array name="products_array">
    <item>Sugar</item>
    <item>Caster sugar</item>
    <item>Salt</item>
</string-array>

And I also have two more string-array lists, with another values: 我还有另外两个字符串数组列表,还有另一个值:

 <string-array name="glass">
    <item>200</item>
    <item>180</item>
    <item>325</item>
</string-array>

and

<string-array name="tableSpoon">
    <item>25</item>
    <item>25</item>
    <item>30</item>
</string-array>

I have created the spinner and this is my onItemSelected method, which, as you can see, shows, which item from products_array list has been selected by users (by using log.i): 我已经创建了微调器,这是我的onItemSelected方法,如您所见,该方法显示了用户(通过使用log.i)选择了products_array列表中的哪个项目:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String selected = parent.getItemAtPosition(position).toString();
    Log.i("Spinner listener", "Item selected " + selected);
}

I have a layout file (fragment), in which I have the spinner and two textViews- one for glass array-list and one for tableSpoon array-list. 我有一个布局文件(片段),其中有微调器和两个textViews-一个用于玻璃阵列列表,一个用于tableSpoon阵列列表。

I want the user to select, lets say, Sugar, and for it to display from string-array glass accordingly 200 in my first TextView, and for the tableSpoon string-array 25 in my second TextView. 我希望用户选择,比如说,Sugar,然后在我的第一个TextView中从字符串数组玻璃中显示它,并在第二个TextView中从tableSpoon字符串数组 25中显示它。

I want these values to be displayed in the exact order, as they all go on the list (Eg., Sugar goes with 200 and 25. Caster sugar goes with 180 and 25 etc.) 我希望这些值以确切的顺序显示,因为它们都在列表中(例如,Sugar分别为200和25。Caster sugar分别为180和25等)

Without doubt, an if-else statement can be used, but given, that I will have quite a few values in all these lists, how can I do this more efficiently? 毫无疑问,可以使用if-else语句,但是假设所有这些列表中都有很多值,那么如何才能更有效地执行此操作?

In your onItemSelected method get the position of the element selected for product array and save it in a variable like this 在您的onItemSelected方法中,获取为产品数组选择的元素的位置,并将其保存在这样的变量中

int index  = spinner1.getSelectedItemPosition(); // spinner1 is product spinner

and then get values from other two arrays for this position and set in your textviews like this 然后从其他两个数组获取该位置的值,并在您的textview中进行这样设置

String [] array1 = getResources().getStringArray(R.array.glass);
  String [] array2 = getResources().getStringArray(R.array.tablespoon);

and then set values in your textviews 然后在您的textviews中设置值

 textview1.setText(array1[index]);
    textview2.setText(array2[index]);

1. Read string-array glass and tableSpoon from XML and store it into two variable as below: 1.从XML读取字符串数组glasstableSpoon并将其存储到两个变量中,如下所示:

String [] glass = getResources().getStringArray(R.array.glass);
String [] tablespoon = getResources().getStringArray(R.array.tablespoon);

2. Inside onItemSelected() method, get the selected item position and use this position to get the value from array glass and tableSpoon . 2.onItemSelected()方法中,获取选定的项目position并使用此position从array glasstableSpoon获取value

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    ..........
    ..............

    // Set text
    firstTextView.setText(glass[position]);
    secondTextView.setText(tableSpoon[position]);
}

Hope this will help~ 希望这会有所帮助〜

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

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