Android:基于两个微调器选择组合的操作

Android: Action based on combination of two spinner selections

我有一个带有两个旋转器的 activity。一个是头发的类型,另一个是头发的颜色。我想要一张图片(在头上)根据从微调器中选择的两个项目进行更改。

这是它应该做的:

  1. Activity 已加载(微调器设置为 "Bald" 和 "Black")
    • 头发秃了
  2. 将头发类型更改为 'Male Short'
    • 头发变成黑色短发。
  3. 将颜色更改为 'Blonde'
    • 头发变成金色短发。

但是第三步不行!!!

这是我的代码:

public class HeadZoom extends MainActivity implements AdapterView.OnItemSelectedListener{

private Spinner hairSpinner;
private Spinner hairColor;
private String type;
private String color;
private ImageView imageView;
private List<String> hairTypeArray = new ArrayList<String>();
private List<String> hairColorArray = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.head_zoom);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    imageView = (ImageView) findViewById(R.id.head);
    type="Bald";

    hairSpinner = (Spinner) findViewById(R.id.hairSpinner);
    hairSpinner.setOnItemSelectedListener(this);
    List<String> hairTypeArray = new ArrayList<String>();
    hairTypeArray.add("Bald");
    hairTypeArray.add("Female Short");
    hairTypeArray.add("Female Medium");
      (...)
    ArrayAdapter<String> hairTypeAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, hairTypeArray);
    hairTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hairSpinner.setAdapter(hairTypeAdapter);

    color = "Black";

    hairColor = (Spinner) findViewById(R.id.hairColor);
    hairColor.setOnItemSelectedListener(this);
    List<String> hairColorArray = new ArrayList<>();
    hairColorArray.add("Black");
    hairColorArray.add("Blonde");
    hairColorArray.add("Blue");
      (...)
    ArrayAdapter<String> hairColorAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, hairColorArray);
    hairColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hairColor.setAdapter(hairColorAdapter);
}

public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
    if(parent.getId()==hairSpinner.getId())
        type = hairTypeArray.get(position);
    else
        color = hairColorArray.get(position);

    switch(type){
        case "Bald":
            imageView.setImageResource((R.drawable.sw_head));
            break;
        case "Female Short":
            if(color=="Black")
                imageView.setImageResource(R.drawable.fhair_short_black);
            else if(color=="Blonde")
                imageView.setImageResource(R.drawable.fhair_short_blonde);
            else if(color=="Blue")
                imageView.setImageResource(R.drawable.fhair_short_blue);
            else if(color=="Brown")
                (...)
                break;
        case "Female Medium":
            if(color=="Black")
                imageView.setImageResource(R.drawable.fhair_med_black);
            else if(color=="Blonde")
                imageView.setImageResource(R.drawable.fhair_med_blonde);
            else if(color=="Blue")
                imageView.setImageResource(R.drawable.fhair_med_blue);
            else if(color=="Brown")
                (...)
                break;
        case "Female Long":
               (...)
        default:
            break;
    }
}

public void onNothingSelected(AdapterView<?> parent){

}

我需要能够同时知道在两个微调器上选择了什么。

我在 activity 时收到此错误报告(顺便说一下,一旦 activity 打开它就会崩溃)

    04-27 15:39:17.165 9934-9934/cameron.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: cameron.myapplication, PID: 9934
                                                                 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                                     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                     at java.util.ArrayList.get(ArrayList.java:308)
                                                                     at cameron.myapplication.HeadZoom.onItemSelected(HeadZoom.java:83)
                                                                     at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
                                                                     at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
                                                                     at android.widget.AdapterView.-wrap1(AdapterView.java)
                                                                     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是因为您正在使用 == 运算符比较两个 String 对象。如果在两个 String 对象之间使用 == 运算符,它将检查两个字符串是否具有相同的引用,而不管它们持有的值如何。

你需要这样比较:

if(color.equalsIgnoreCase("Black"))

if(color.equals("Black"))

这将检查字符串对象值而不是其引用。

正如@sasikumar 建议的那样,还删除了局部变量

List<String> hairColorArray = new ArrayList<>();
List<String> hairTypeArray = new ArrayList<String>();

原因是您已经在全局中初始化 ArrayList。

 private List<String> hairTypeArray = new ArrayList<String>();
 private List<String> hairColorArray = new ArrayList<String>();

所以在 oncreate 方法中它只采用全局变量 values.but 你只添加了值 local arraylist oncreate method.so 在 oncreate 方法中删除以下代码

List<String> hairColorArray = new ArrayList<>();
List<String> hairTypeArray = new ArrayList<String>();