如何加载与微调器选择相关的文本字符串

时间:2016-01-19 21:00:32

标签: java android xml android-studio spinner

我有一个微调器显示数组中的列表,我想要做的是让所选选项在数组下方的窗口中显示更详细的选项。

我之后的原因是我想构建一个角色创建工具,我希望人们能够从一个选项列表中选择,然后根据他们选择的选项显示该选择的含义。

到目前为止,我在strings.xml中使用了我的数组:

<string-array name="humanBackgroundData">
    <item>Cultist</item>
    <item>Freed Slave</item>
    <item>Gangster</item>
    <item>Techno-Reaper</item>
    <item>Tribal</item>
    <item>Wanderer</item>
    <item>Dwelled in the Dwelling</item>
    <item>Feral Child</item>
    <item>Welsh/Scottish Descendant</item>
</string-array>

我的Spinner在activity.xml中:

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinB"
    android:entries="@array/humanBackgroundData"
    android:paddingBottom="20dp"
    >

我一直在寻找一种方法将选定的选项链接到另一个字符串,然后可以显示该选择的其他数据。我想这样做是因为在微调器中有大量数据看起来很糟糕。我已经看到了为微调器编号的方法,所以第一个选择将是1,第二个2,所以我相信我应该能够使用它作为调用字符串的引用,但我只使用android studio两个左右几个星期,我找不到任何其他关于这样做的帖子。有什么建议吗?

感谢您的时间:)

更新

我现在已经部分找到了一个解决方案,如果我在活动中包含它,我现在可以让TextView引用该字符串,但是我想从strings.xml中提取它,这样它会使事情变得更加整洁并阻止我重复我自己。我在下面链接了我的代码。如果我将“textnone”替换为“text1”,那么它可以工作,所以我认为这只是我链接字符串数组的方式的问题。

    <string-array name="humanBackgroundData">
    <item>Cultist</item>
    <item>Freed Slave</item>
    <item>Gangster</item>
    <item>Techno-Reaper</item>
    <item>Tribal</item>
    <item>Wanderer</item>
    <item>Dwelled in the Dwelling</item>
    <item>Feral Child</item>
    <item>Welsh/Scottish Descendant</item>
</string-array>


package com.studios.grimvoodoo.spicedcharacterbuilder;

import android.content.res.Resources;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class HumanActivity extends Activity {

private static Button proceed;

Resources res = getResources();
String[] text1 = res.getStringArray(R.array.ghulBackgroundData);


String[] textnone = {"human", "ghul", "postgen",
        "gnome", "pixie", "giant", "vampire"};

String[] text2 = {"standard", "undead", "super mutant", "wee man", "flying    wee man", "big and hungry", "dead and loving it"};

Spinner spinner1;
TextView textView1;

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

    textView1 = (TextView) findViewById(R.id.text1);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 =
            new ArrayAdapter<String>(HumanActivity.this,
                    android.R.layout.simple_spinner_item, text1);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}

AdapterView.OnItemSelectedListener onItemSelectedListener1 =
        new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String s1 = String.valueOf(text2[position]);
                textView1.setText(s1);
            }

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

        };
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.studios.grimvoodoo.spicedcharacterbuilder.HumanActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:autoLink="web"
    android:text="http://android-er.blogspot.com/"
    android:textStyle="bold" />

<Spinner
    android:id="@+id/spinner0"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/text0"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/text2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>


    <string-array name="humanBackgroundData">
    <item>Cultist</item>
    <item>Freed Slave</item>
    <item>Gangster</item>
    <item>Techno-Reaper</item>
    <item>Tribal</item>
    <item>Wanderer</item>
    <item>Dwelled in the Dwelling</item>
    <item>Feral Child</item>
    <item>Welsh/Scottish Descendant</item>
</string-array>

1 个答案:

答案 0 :(得分:0)

如果我理解你的话,你应该在布局中在Spinner下放置额外的TextView。然后使用其他信息(info数组)创建单独的字符串数组,其大小与现有字符串数组相同。

然后在spinner的onItemSelected(AdapterView父,View视图,int pos,long id)方法中,获取所选spinner元素(pos)的位置,并使用infos数组中的相应元素更新TextView(带索引pos的元素) )。