这是我的Spinner
代码,它将我的SQL数据库中的员工姓名作为对象加载并在屏幕上显示:
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
employeeList = new int[list.size()];
//loop to fill the list
nameList[0] = "All Employees";
employeeList[0] = 0;
for (int i=0; i<list.size(); i++) {
nameList[i+1] = list.get(i).getName() + " " + list.get(i).getSurname();
employeeList[i+1] = list.get(i).getID();
}
//creating adapter for spinner
ArrayAdapter<String > dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, nameList);
//drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_multichoice);
//attaching data adapter to spinner
selectName.setAdapter(dataAdapter);
}
我正在尝试将“All Employees”文本作为第一个选项包含在内,因为我想实现一个If
语句,当用户选择“所有员工”选项时,该应用程序将获取某些特定数据,当用户选择特定员工时,例如“John Smith”应用程序将仅为该记录获取数据。
上面的代码段是我到目前为止所得到的,我收到ArrayIndexOutOfBoundsException
错误。
我的LogCat显示了这个:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lysandroslysandrou.marstest4/com.example.lysandroslysandrou.marstest4.Reports}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at com.example.lysandroslysandrou.marstest4.Reports.loadNames(Reports.java:147)
at com.example.lysandroslysandrou.marstest4.Reports.onCreate(Reports.java:78)
第147行是nameList[i+1] = list.get(i).getName() + " " + list.get(i).getSurname();
我尝试了很多其他方法,但都没有。从我的数据库加载保存为“所有员工”的值也不是一个可行的解决方案。
我正在寻找两件事:
A)为什么我特别在我的情况下会收到此错误? (我已经读过这个例外here和here,但无法理解为什么会发生这种情况)
和 B)我的问题有解决方案吗?
注意:如果删除了行nameList[0] = "All Employees
和employeeList[0] = "0"
,则代码段正常。
编辑:我的新堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getDropDownView(AppCompatSpinner.java:624)
at android.support.v7.widget.AppCompatSpinner$DropDownAdapter.getView(AppCompatSpinner.java:620)
at android.support.v7.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:547)
at android.support.v7.widget.AppCompatSpinner.access$500(AppCompatSpinner.java:65)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:738)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:763)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:19749)
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)
编辑2:这是我的onItemSelected
方法:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//On selecting a spinner item
String list = parent.getItemAtPosition(position).toString();
//employeeId = employeeList[position];
//showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + list, Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
您需要更改
//list for storing the names
String[] nameList = new String[list.size()];
//list for storing the ID and keeping it hidden
employeeList = new int[list.size()];
到
//list for storing the names
String[] nameList = new String[list.size() + 1];
//list for storing the ID and keeping it hidden
employeeList = new int[list.size() + 1];
因为您需要为第一个“所有员工”元素分配一个项目。
当您尝试访问namesList[i + 1]
元素时i == list.size() - 1
,i + 1 == list.size()
时,您会得到一个解释。例如,当您仅使用namesList[5]
0..4
个元素时,请访问String[] nameList = new String[list.size()];
我评论了几行,因为我没有你的db。但以下代码对我来说很好:
private void loadSpinner(Spinner spinner, ArrayList<Integer> items) {
//list for storing the names
String[] nameList = new String[items.size() + 1];
//list for storing the ID and keeping it hidden
//employeeList = new int[list.size()];
//loop to fill the list
nameList[0] = "All Employees";
//employeeList[0] = 0;
for (int i = 0; i < items.size(); i++) {
nameList[i + 1] = String.valueOf(i);
}
//creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, nameList);
//drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.select_dialog_multichoice);
//attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}