请仔细阅读,不要将此问题标记为重复。我有2个Spinner
元素,其中一个元素在我的应用中使用硬编码填充了String
个值,另一个元素使用ArrayList
从我的数据库中填充。 2个Spinners被称为名称和疾病。
名称代码 Spinner
:
//method to fill the spinner with data
private void loadNames() {
//database handler
LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());
List<DataBean> list = db.getAllDat();
//list for storing the names
String[] nameList = new String[list.size()];
//list for storing the ID and keeping it hidden
employeesList = new int[list.size()];
for (int i=0; i<list.size(); i++) {
employeesList[i] = list.get(i).getID();
nameList[i] = list.get(i).getName() + " " + list.get(i).getSurname();
}
//creating adapter for spinner
ArrayAdapter<String > dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, nameList);
//drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching data adapter to spinner
selectName.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String list = parent.getItemAtPosition(position).toString();
employeeId = employeesList[position];
//showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + list, Toast.LENGTH_LONG).show();
}`
在此列表中,我使用姓氏和ID传递我的员工姓名,并通过使用getter和setter类来实现。
我的getter和setters类:
public class DataBean implements Serializable {
//employee fields
protected int _id;
protected String name;
protected String surname;
protected String dateofbirth;
protected String department;
protected String workplace;
protected String number;
protected String bnumber;
protected String email;
protected String address;
protected String notes;
public DataBean (int _id, String name, String surname, String dateofbirth, String department, String workplace, String number, String bnumber, String email, String address, String notes ) {
this._id = _id;
this.name = name;
this.surname = surname;
this.dateofbirth = dateofbirth;
this.department = department;
this.workplace = workplace;
this.number = number;
this.bnumber = bnumber;
this.email = email;
this.address = address;
this.notes = notes;
}
public DataBean (String name, String surname, String department, String workplace) {
this.name = name;
this.surname = surname;
this.department = department;
this.workplace = workplace;
}
public int getID() {return this._id;}
public int setID(int id) {return this._id = _id;}
public String getName() {return this. name;}
public String getSurname() {return this.surname;}
public String getDateofbirth() {return this.dateofbirth;}
public String getDepartment() {return this.department;}
public String getWorkplace() {return this.workplace;}
public String getNumber() {return this.number;}
public String getBnumber() {return this.bnumber;}
public String getEmail() {return this.email;}
public String getAddress() {return this.address;}
public String getNotes() {return this.notes;}
}
当我的疾病Spinner
发挥作用时,会发生奇怪的事情。你看,我通过一个不同的形式在我的应用程序中添加员工,然后他们被传递到这里用于其他目的。
此处的交易:例如,我的名称 spinner
中有5个值,而疾病微调器中有10个值,我如果我选择让我们的名称微调器中的第5个值和我的疾病微调器中的第6个值,我将获得ArrayIndexOutOfBoundsException
。如果我在疾病微调器中选择第7,第8,第9个值等,也会发生同样的情况。因此,疾病 Spinner
中选择的任何内容都大于名称的大小会导致我的应用程序崩溃并给我异常。
我不知道为什么会发生这种情况,因为我的数组没有固定的大小长度。我已经阅读了有关文档here和here的所有内容,我了解数组是如何工作的,并且它们是从位置0而不是1开始索引的。我还搜索了Stack Overflow以获得答案引导我to this answer和this one等等,但并没有真正为我提供答案。我甚至多次阅读this answer,这让我理解了异常是如何引起的,但我的情况有所不同。
此行发生异常:employeeId = employeesList[position];
如果我删除此行,我的代码工作正常,但我的应用程序的目的受到损害。这样做的目的是通过从名单中选择员工姓名和疾病列表中的疾病类型来增加员工的病情。如果我有3名员工和8名病人,如果我选择第4或第5次疾病,我将无法做到这一点。任何帮助是极大的赞赏。
这是我的logcat:
java.lang.ArrayIndexOutOfBoundsException: length=4; index=6
at com.example.lysandroslysandrou.marstest4.AddSicknessForm.onItemSelected(AddSicknessForm.java:160)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)
at android.widget.AdapterView.access$200(AdapterView.java:48)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:865)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)