gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:05:15 25 4
gpt4 key购买 nike

我有一个有两个旋转器的 Activity 。一是头发的类型,二是头发的颜色。我希望根据从微调器中选择的两个项目来更改图片(头部)。

它应该执行以下操作:

  1. Activity 已加载(旋转器设置为“Bald”和“Black”)
    • 头发秃了。
  2. 将头发类型更改为“男性短发”
    • 头发变为黑色短发。
  3. 将颜色更改为“金发”
    • 头发变为男性金色短发。

但是第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){

}

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

当 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)

最佳答案

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

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

因此 oncreate 方法仅采用全局变量值。但是您仅在 oncreate 方法中添加了本地 arraylist 值。因此在 oncreate 方法中删除以下代码

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

关于java - Android:基于两个微调器选择组合的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36953214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com