我经历了这么多代码并试图弄清楚我做错了什么但是我不及时而且浪费时间。这很简单,但我无法理解......
我正在开发一个Android应用程序,需要在一个活动上放置2个微调器。第二个微调器将根据第一个微调器上选择的条目填充。听起来很简单吧?
所以这就是我想做的事情。如果用户选择“移动”,则应显示choosePersonalPlan
中的内容,当用户选择“大型商业chooseLargeBusplan
应运行且最终条目应调用chooseSmallMedPlan
时。我得到的只是ChooseSmallMedPlan
一直显示的项目。
继承我的代码。请让我知道我做错了!
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 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>
答案 0 :(得分:0)
略 choosePersonalPlan(); chooseLargeBusPlan(); chooseSmallMedPlan();
来自onCreate()方法的
答案 1 :(得分:0)
添加
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
}
}
}
答案 2 :(得分:0)
在您的代码中,您没有将planOnClickListener()
设置在任何位置。我忘了设置像catspinner.setOnItemSelectedListener(new planOnClickListener());