微调器值基于父微调器值更改而更改

时间:2013-03-26 06:03:40

标签: android

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

2 个答案:

答案 0 :(得分:3)

你必须像以下那样设置适配器:

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>

答案 1 :(得分:3)

可能会有所帮助,

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。

同样遵循选择的第二个微调器项目。