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

时间:2016-04-30 09:36:30

标签: java android android-studio android-spinner android-image

我有两个旋转器的活动。一种是头发的类型,另一种是头发的颜色。我希望根据从微调器中选择的两个项目来改变图片(在头上)。

这是它应该做的:

  1. 活动已加载(旋转器设置为“Bald”和“Black”)
    • 头发是光头。
  2. 将发型改为'男短'
    • 头发变成短的黑色男性头发。
  3. 将颜色更改为'Blonde'
    • 头发变成短金发男性头发。
  4. 但第3步不起作用!!!

    继承我的代码:

    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){
    
    }
    

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

    我在活动时收到此错误报告(btw在活动打开后立即崩溃)

        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)
    

2 个答案:

答案 0 :(得分:2)

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

你需要像这样比较:

if(color.equalsIgnoreCase("Black"))

OR

if(color.equals("Black"))

这将检查String对象值而不是其引用。

正如@sasikumar建议也删除那个局部变量

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

答案 1 :(得分:2)

原因是已经初始化全局的ArrayList。

BABA

因为oncreate方法只取全局变量值。但是你只添加了本地arraylist oncreate方法的值。所以在oncreate方法中删除以下代码

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