编程语言
首页 > 编程语言> > java – 使用一个依赖于另一个的两个android微调器

java – 使用一个依赖于另一个的两个android微调器

作者:互联网

我经历了这么多代码,并试图弄清楚我做错了什么,但我不及时,浪费时间.这很简单,但我无法弄清楚..

我正在开发一个Android应用程序,需要在一个活动上放置2个微调器.第二个微调器将根据第一个微调器上选择的条目填充.听起来很简单吧?

所以这就是我想做的事情.如果用户选择Mobile,则应显示choosePersonalPlan中的内容,当用户选择Large business时,chooseLargeBusplan应该运行,最后一个条目应该调用chooseSmallMedPlan.我得到的只是ChooseSmallMedPlan一直显示的项目.

继承我的代码.我告诉我我做错了!

Java文件

package com.example.mobilebillforecaster;

import java.util.ArrayList;
import java.util.List;

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


 public class Du_postpaid extends Activity {

    public Spinner catspinner,planspinner3,planspinner1,planspinner2;


    protected void onCreate(Bundle mbfSplash) {
            super.onCreate(mbfSplash);
            setContentView(R.layout.postpaid);

    chooseCategory();
    choosePersonalPlan();
    chooseLargeBusPlan();
    chooseSmallMedPlan();
    }

    public void chooseCategory() {

        catspinner = (Spinner) findViewById(R.id.catspinner);
        ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(this, R.array.ducategory, android.R.layout.simple_spinner_item);
        catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        catspinner.setAdapter(catAdapter);

    }
    private void choosePersonalPlan(){
        planspinner1 = (Spinner) findViewById(R.id.planspinner);
        List<String> planlist = new ArrayList<String>();
        planlist.add("Elite Regular Plan");
        planlist.add("Elite Super Plans");
        planlist.add("Emirati Plan");
        planlist.add("Pay As You Go");
        planlist.add("Visitor Mobile Line");
        ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist);
        planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        planspinner1.setAdapter(planAdapter1);

    }

    private void chooseLargeBusPlan(){

        planspinner2 = (Spinner) findViewById(R.id.planspinner);
        List<String> planlist1 = new ArrayList<String>();
        planlist1.add("Premier Plan");
        planlist1.add("The Executive Plan");
        planlist1.add("Business Circle Plan");
        planlist1.add("Business Diamond Plan");
        ArrayAdapter<String> planAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist1);
        planAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        planspinner2.setAdapter(planAdapter2);


    }
    private void chooseSmallMedPlan(){

        planspinner2 = (Spinner) findViewById(R.id.planspinner);
        List<String> planlist2 = new ArrayList<String>();
        planlist2.add("Business Super Plan");
        planlist2.add("Her Business Super Plan");
        planlist2.add("Business Circle Plan");
        planlist2.add("Pay As you Go® Plus");
        ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist2);
    planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    planspinner2.setAdapter(planAdapter3);

    }


    public class planOnClickListener implements OnItemSelectedListener {

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

                parent.getItemAtPosition(pos);  

                if (pos==0){
                choosePersonalPlan();   
                }else if (pos==1){
                chooseLargeBusPlan();
                } else if (pos==2){
                chooseSmallMedPlan();
}
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
            }


 }

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="left" >

    <TextView
        android:id="@+id/postp"
        android:layout_width="130dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:text="@string/postp"
        android:textSize="30dp"

        />


    <Spinner
        android:id="@+id/catspinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/postp"
        android:layout_marginTop="25dp"
        android:entries="@array/ducategory"
        android:prompt="@string/category_prompt"
        />

     <Spinner
        android:id="@+id/planspinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/catspinner" />


</RelativeLayout>

解决方法:

catspinner.setOnItemSelectedListener(new planOnClickListener());

chooseCategory();

这是整个代码 –

public class TemoActivity extends Activity {

public Spinner catspinner,planspinner3,planspinner1,planspinner2;


protected void onCreate(Bundle mbfSplash) {
        super.onCreate(mbfSplash);
        setContentView(R.layout.main);

chooseCategory();
choosePersonalPlan();
chooseLargeBusPlan();
chooseSmallMedPlan();
}

public void chooseCategory() {

    catspinner = (Spinner) findViewById(R.id.catspinner);
    ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(this, R.array.ducategory, android.R.layout.simple_spinner_item);
    catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    catspinner.setAdapter(catAdapter);
    catspinner.setOnItemSelectedListener(new planOnClickListener());
}
private void choosePersonalPlan(){
    planspinner1 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist = new ArrayList<String>();
    planlist.add("Elite Regular Plan");
    planlist.add("Elite Super Plans");
    planlist.add("Emirati Plan");
    planlist.add("Pay As You Go");
    planlist.add("Visitor Mobile Line");
    ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, planlist);
    planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    planspinner1.setAdapter(planAdapter1);

}

private void chooseLargeBusPlan(){

    planspinner2 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist1 = new ArrayList<String>();
    planlist1.add("Premier Plan");
    planlist1.add("The Executive Plan");
    planlist1.add("Business Circle Plan");
    planlist1.add("Business Diamond Plan");
    ArrayAdapter<String> planAdapter2 = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, planlist1);
    planAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    planspinner2.setAdapter(planAdapter2);


}
private void chooseSmallMedPlan(){

    planspinner2 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist2 = new ArrayList<String>();
    planlist2.add("Business Super Plan");
    planlist2.add("Her Business Super Plan");
    planlist2.add("Business Circle Plan");
    planlist2.add("Pay As you Go® Plus");
    ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, planlist2);
planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner2.setAdapter(planAdapter3);

}


public class planOnClickListener implements OnItemSelectedListener {

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

            parent.getItemAtPosition(pos);  

            if (pos==0){
            choosePersonalPlan();   
            }else if (pos==1){
            chooseLargeBusPlan();
            } else if (pos==2){
            chooseSmallMedPlan();
 }
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
        }
}

标签:android,java,android-activity,android-spinner
来源: https://codeday.me/bug/20191007/1865276.html