1

大家好,我有三个微调器。我想根据第一个微调器值更改第二个微调器值,并根据第二个微调器值更改第三个微调器值,所有值都是从数据库中获取和显示的。我正在使用 ArrayAdapter。我正在搜索最后两周。但我没有得到解决方案,请帮助我,我的时间不多了。

4

2 回答 2

4

你只需要像这样设置适配器:

package com.example.mapsdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

    private Spinner spin1;
    private Spinner spin2;

    ArrayList<String> a = new ArrayList<String>();
    ArrayList<String> b = new ArrayList<String>();
    ArrayList<String> c = new ArrayList<String>();

    // private ImageView imageView;

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

        spin1 = (Spinner) findViewById(R.id.spinner1);
        spin2 = (Spinner) findViewById(R.id.spinner2);

        fillarray();

        spin1.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_dropdown_item_1line, a));
        /*
         * spin2.setAdapter(new ArrayAdapter<String>(MainActivity.this,
         * android.R.layout.simple_dropdown_item_1line, b));
         */

        spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // TODO Auto-generated method stub
                if (pos == 0) {
                    spin2.setAdapter(new ArrayAdapter<String>(
                            MainActivity.this,
                            android.R.layout.simple_dropdown_item_1line, b));
                } else {
                    spin2.setAdapter(new ArrayAdapter<String>(
                            MainActivity.this,
                            android.R.layout.simple_dropdown_item_1line, c));
                }
            }

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

            }
        });
    }

    private void fillarray() {
        // TODO Auto-generated method stub
        a.clear();
        a.add("a");
        a.add("b");
        a.add("c");
        a.add("d");
        a.add("e");

        b.clear();
        b.add("1");
        b.add("2");
        b.add("3");
        b.add("4");

        c.clear();
        c.add("Android");
        c.add("ios");
    }

}

您的布局文件,如:

<RelativeLayout 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" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="80dp" />

</RelativeLayout>
于 2013-03-26T06:13:21.367 回答
4

可能这会有所帮助,

ArrayAdapter<String> secondspinnerAdapter;
ArrayAdapter<String> firstspinnerAdapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_spinner_item,
                firstspinnervalue);
        first_spinner.setAdapter(firstspinnerAdapter);
        firstspinnerAdapter 
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


firstspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

                    // get data from database add to arraylist

                      secondspinnerAdapter = new
                      ArrayAdapter<String>( mainActivity.this,
                      android.R.layout.simple_spinner_item,   arraylistvalue);
                      second_spinner.setAdapter(secondspinnerAdapter);
                      second_spinner.setSelection(arg2, false);
                      secondspinnerAdapter.setDropDownViewResource
                      (android.R.layout.simple_spinner_dropdown_item);

            }

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

            }
        });

选择第一个微调器项目,然后从数据库中获取数据并填充 arraylist 并设置 secondspinner。

以同样的方式跟随选择的第二个微调器项目。

于 2013-03-26T06:29:42.173 回答