在微调器中选择项目后更新内容

发布于 2024-08-21 16:39:03 字数 250 浏览 5 评论 0原文

又是我。我在过去的几个小时里尝试了如何更改微调器的内容。 好吧,让我们从头开始。

我有三个旋转器。它们都有初始值。第一个旋转器是主旋转器,其他两个旋转器取决于第一个旋转器中选择的值。因此,我想在第一个微调器中进行选择后更新最后两个微调器。 *编辑:所有旋转者都在进行相同的活动。

我怎样才能做到这一点?我的问题是我只能在 itemselectadapter 上的微调器中进行更改,但这是一个全新的类。我无法到达其他旋转器所在的活动。

谢谢

its me again. I tried the last hours, how to change content of a spinner.
ok, lets start from the beginning.

I have three spinners. They all have initial values. The first spinner is the main spinner and the other two spinners depend on the vale chosen in the first one. So i want to update the last two spinners after making a selection in spinner one. *edit: All spinners are on the same activity.

How can i achieve this? My problem is that i can only make changes in the spinners onitemselectadapter but thats a whole new class. I cannot reach the activity where my other spinners are.

thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-08-28 16:39:03

您的旋转者从事不同的活动吗?

如果是,那么您可以通过 Intent(请参阅 putExtra 部分)并从下一个活动中检索值,以便您可以相应地设置下一个微调器。

编辑:

这是一个更改第二个和第三个微调器中所选项目的示例。 更新侦听器(onItemSelected 方法)

使用您的逻辑Activity

private Spinner s;
private Spinner s2;
private Spinner s3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    String[] myList = new String[] { "Hello", "World", "Foo", "Bar" };
    String[] myList2 = new String[] { "Hello2", "World2", "Foo2", "Bar2" };
    String[] myList3 = new String[] { "Hello3", "World3", "Foo3", "Bar3" };

    s = (Spinner) findViewById(R.id.spinner1);
    s2 = (Spinner) findViewById(R.id.spinner2);
    s3 = (Spinner) findViewById(R.id.spinner3);

    s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList));
    s2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList2));
    s3.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList3));


    s.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int pos, long id) {
            s2.setSelection(pos);
            s3.setSelection(pos);
        }

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


        }});
}

: main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="wrap_content"
        android:orientation="vertical">
<Spinner android:id="@+id/spinner1" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Spinner android:id="@+id/spinner2" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Spinner android:id="@+id/spinner3" android:layout_height="wrap_content" android:layout_width="fill_parent" />
</LinearLayout>

Are your spinners in different activities?

If they are, so you can just pass the selected value of the first spinner via Intent (See the putExtra section) and retrieve the value from the next activity so that you can set accordingly the next spinners.

Edit:

Here is an example that changes the selected item in the 2nd and 3rd spinner. Update the listener (onItemSelected method) with your logic

Activity:

private Spinner s;
private Spinner s2;
private Spinner s3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    String[] myList = new String[] { "Hello", "World", "Foo", "Bar" };
    String[] myList2 = new String[] { "Hello2", "World2", "Foo2", "Bar2" };
    String[] myList3 = new String[] { "Hello3", "World3", "Foo3", "Bar3" };

    s = (Spinner) findViewById(R.id.spinner1);
    s2 = (Spinner) findViewById(R.id.spinner2);
    s3 = (Spinner) findViewById(R.id.spinner3);

    s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList));
    s2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList2));
    s3.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList3));


    s.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int pos, long id) {
            s2.setSelection(pos);
            s3.setSelection(pos);
        }

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


        }});
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="wrap_content"
        android:orientation="vertical">
<Spinner android:id="@+id/spinner1" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Spinner android:id="@+id/spinner2" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Spinner android:id="@+id/spinner3" android:layout_height="wrap_content" android:layout_width="fill_parent" />
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文