java - Android 第二个微调器根据第一个微调器中的选择进行更改

标签 java android dynamic spinner

首先,是的,我已经浏览了许多可能的解决方案,它们让我接近了,但我错过了一些我一生都找不到的关键小步骤。一切都很好。第一个微调器(类别)按应有的方式填充,我可以告诉第二个微调器(颜色)正在通过创建方法填充,但我错过了告诉它在用户选择类别时重新创建自身的位置。这是我现在的所有代码。

package yarn.pack.name;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.AdapterView.OnItemSelectedListener;

public class YarnDatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner Category_Add_Spinner = Create_Category_Add_Spinner();
        Spinner Color_Add_Spinner = Create_Color_Add_Spinner(null);
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
        e.printStackTrace();
    }
}

// Creates the Category Spinner for the Add section sends the choice made to
// a method for populating the color spinner
public Spinner Create_Category_Add_Spinner() {
    Spinner Category_Add_Spinner = (Spinner) findViewById(R.id.categoryAddID);
    String[] Category_Add_Spinner_Array = getResources().getStringArray(
            R.array.categoryNames);
    SpinnerAdapter Category_Add_Spinner_Adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_dropdown_item,
            Category_Add_Spinner_Array);
    Category_Add_Spinner.setAdapter(Category_Add_Spinner_Adapter);
    Category_Add_Spinner
            .setOnItemSelectedListener(new Category_Add_Spinner_Listener());
    return Category_Add_Spinner;
}

// The listener for the Category Spinner that sends whatever choice made to
// the method that populates the second spinner.
public class Category_Add_Spinner_Listener implements
        OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> Category_Add_Adapter_View,
            View v, int position, long row) {
        String Category_Add_Choice = Category_Add_Adapter_View
                .getItemAtPosition(position).toString();
        Create_Color_Add_Spinner(Category_Add_Choice);
    }

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

    }

}

// Creates the Color Spinner for the Add section
public Spinner Create_Color_Add_Spinner(String category_Add_Choice) {
    String[] Color_Add_Spinner_Array = null;
    Spinner Color_Add_Spinner = (Spinner) findViewById(R.id.colorAddID);
    if (category_Add_Choice == "Red") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.RedColors);
    } else if (category_Add_Choice == "Pink") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.PinkColors);
    } else if (category_Add_Choice == "Orange") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.OrangeColors);
    } else if (category_Add_Choice == "Yellow") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.YellowColors);
    } else if (category_Add_Choice == "Green") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.GreenColors);
    } else if (category_Add_Choice == "Blue") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.BlueColors);
    } else if (category_Add_Choice == "Purple") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.PurpleColors);
    } else if (category_Add_Choice == "Neutral") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.NeutralColors);
    } else if (category_Add_Choice == "Mix") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.MixColors);
    } else if (category_Add_Choice == "Fleck") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.FleckColors);
    } else
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.MixColors);     
    SpinnerAdapter Color_Add_Spinner_Adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_dropdown_item,
            Color_Add_Spinner_Array);
    Color_Add_Spinner.setAdapter(Color_Add_Spinner_Adapter);
    Color_Add_Spinner
            .setOnItemSelectedListener(new Color_Add_Spinner_Listener());
    return Color_Add_Spinner;
}
}

我非常确定我遗漏的任何东西都非常明显,但我已经为此工作了很长时间,而我的时间很短,所以我想我应该伸出手来看看我是否能做得到”不要让一些光线照在这个主题上。提前感谢你们能给我的任何帮助。


对命名约定感到抱歉。我通常会这样命名,所以它对我自己最有意义。无论如何,事实证明问题出在我使用 == 而不是 .equals 进行了更改,并且一切顺利!谢谢大家的帮助。

最佳答案

你的代码几乎可以工作,不是很完美,但它可以很快工作:

  1. 永远不要使用 == 来比较 java 中的字符串。始终使用 stringA.equals( stringB )。我想这是你唯一的问题。
  2. 尊重 java 命名约定,这样更容易阅读。

正如@Frankenstein 所指出的,与始终使用 findViewById 从布局中重新获取它们相比,使您的小部件数据成员通常更加清晰和高效。

关于java - Android 第二个微调器根据第一个微调器中的选择进行更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10345107/

相关文章:

android - 如何使用 Intellij IDEA 中的 UMLGraph 创建 javadoc

java - 使用 SQLiteOpenHelper onCreate() 方法时向 sqlite 插入 2 行

android - 如何解析从 EditText 到 TextView 的 double ? (安卓)

matlab - 在 MATLAB 中动态更改 for 循环

c# - 动态执行方法

java - 无法从 Java 程序内部调用 "gcc"

java - 在具有不断重新绘制的 JPanel 的 JApplet 中使用组件

java - 在 Java 中将对象写入文件时内存不足

java - 如何更改 JPanel 的字体大小

python - 如何使用 Python 3 元类动态生成中间类