我在片段中设置了以下android spinner:
ArrayList<String> subjects=new ArrayList<String>();
subjects.add("Option0");
subjects.add("Option1");
subjects.add("Option2");
subjects.add("Option2");
Spinner ExamSpinner = (Spinner)rlayout2.findViewById(R.id.SpinnerExam);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
ArrayAdapter <String> subjectAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_spinner_item, subjects);
subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listview.setAdapter(subjectAdapter);
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch(adapterView.getId()){
case 0:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
它正在提供IndexOutOfBoundsException。 logcat如下:
12-30 00:52:19.020: E/AndroidRuntime(16589): FATAL EXCEPTION: main
12-30 00:52:19.020: E/AndroidRuntime(16589): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
12-30 00:52:19.020: E/AndroidRuntime(16589): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-30 00:52:19.020: E/AndroidRuntime(16589): at java.util.ArrayList.get(ArrayList.java:304)
12-30 00:52:19.020: E/AndroidRuntime(16589): at com.gloxci.studentreport.ExamScheduleFragment$10.onItemClick(ExamScheduleFragment.java:359)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.widget.AbsListView$1.run(AbsListView.java:3529)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.os.Handler.handleCallback(Handler.java:615)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.os.Looper.loop(Looper.java:137)
12-30 00:52:19.020: E/AndroidRuntime(16589): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-30 00:52:19.020: E/AndroidRuntime(16589): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 00:52:19.020: E/AndroidRuntime(16589): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 00:52:19.020: E/AndroidRuntime(16589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-30 00:52:19.020: E/AndroidRuntime(16589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-30 00:52:19.020: E/AndroidRuntime(16589): at dalvik.system.NativeStart.main(Native Method)
12-30 00:54:24.602: E/Trace(17678): error opening trace file: No such file or directory (2)
我知道这与侦听器有关,因为它可以正常工作,但是当我点击微调器上的某个项目时,会导致应用程序崩溃。
我不确定这条线:
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
正如我通常使用的那样;
ExamSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
为微调器设置侦听器,但这导致它崩溃,我不得不使用
listview.setAdapter(SubjectAdapter)
让它首先显示微调器。
所以我的问题是,为什么它会给出一个OutOfBoundsException,只要我能看到一切都设置正确。
以下是全班:
package com.gloxci.studentreport;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import com.gloxci.studentreport.database.DBAdapter;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class ExamScheduleFragment extends CustomFragment{
int mYear;
int mMonth;
int mDay;
TextView mDateDisplay;
//CustomViewPager vp;
DBAdapter dbAdapter;
String examTitle="";
ArrayList<StudentData> students=new ArrayList<StudentData>();
ArrayList<String> exams=new ArrayList<String>();
ArrayList<String> subexams=new ArrayList<String>();
ArrayList<String> EIDs=new ArrayList<String>();
HashMap<String, String> percentages=new HashMap<String, String>();
ArrayAdapter<String> adapterExam;
String selectedExam="";
String total_marks="";
boolean isEdit=false;
int lastPage=0;
ListAdapterPercentage adapter;
public ExamScheduleFragment() {
// Empty constructor required for fragment subclasses
MainActivity.setTitle("Exam Schedule");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewPagerAdapter pAdapter=new ViewPagerAdapter(getActivity(), new RelativeLayout[]{setLayout1(inflater),setLayout2(inflater),setLayout3(inflater),setLayout4(inflater)});
vp=new CustomViewPager(getActivity());
vp.setAdapter(pAdapter);
vp.setPagingEnabled(false);
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
if(lastPage==3 &&arg0==2)
{
vp.setCurrentItem(1);
}
else
{
lastPage=arg0;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
dbAdapter=new DBAdapter(getActivity());
return vp;
}
RelativeLayout setLayout3(LayoutInflater inflater)
{
RelativeLayout rlayout1=(RelativeLayout)inflater.inflate(R.layout.exams, null);
final EditText edMarks=(EditText)rlayout1.findViewById(R.id.edMarks);
ArrayList<String> subjects=new ArrayList<String>();
dbAdapter=new DBAdapter(getActivity());
Cursor mCursor = dbAdapter.AllSubject();
getActivity().startManagingCursor(mCursor);
mCursor.moveToFirst();
while (mCursor.isAfterLast() == false) {
String Code = mCursor.getString(mCursor.getColumnIndexOrThrow("Code"));
String Name = mCursor.getString(mCursor.getColumnIndexOrThrow("Name"));
subjects.add(Code+"-"+Name);
mCursor.moveToNext();
}
final Spinner spSubject=(Spinner)rlayout1.findViewById(R.id.spSubject);
ArrayAdapter<String> subjectAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, subjects);
subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spSubject.setAdapter(subjectAdapter);
final Spinner spSemester=(Spinner)rlayout1.findViewById(R.id.spSemester);
ArrayList<String> semesters=new ArrayList<String>();
mCursor = dbAdapter.AllSemester();
getActivity().startManagingCursor(mCursor);
mCursor.moveToFirst();
while (mCursor.isAfterLast() == false) {
String Name = mCursor.getString(mCursor.getColumnIndexOrThrow("Name"));
semesters.add(Name);
mCursor.moveToNext();
}
ArrayAdapter<String> semesterAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, semesters);
semesterAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spSemester.setAdapter(semesterAdapter);
final TimePicker tp=(TimePicker)rlayout1.findViewById(R.id.timePicker1);
Button btnSave=(Button)rlayout1.findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(edMarks.getText().toString().compareTo("")==0){
Toast.makeText(getActivity(), "Enter the Marks", Toast.LENGTH_SHORT).show();
return;
}
String sem="";
if(spSemester.getSelectedItem()!=null)
{
sem=spSemester.getSelectedItem().toString();
}
total_marks=edMarks.getText().toString();
String value=spSubject.getSelectedItem().toString()+" - "+sem;
value+="\nMarks: "+edMarks.getText().toString()+"\n"+mDay+"/"+mMonth+"/"+mYear+"\n" +tp.getCurrentHour()+":"+tp.getCurrentMinute();
if(tp.getCurrentHour()<12)
{
value+=" AM";
}
else
{
value+=" PM";
}
ShowAlertDialog2(value);
}
});
Button btnDate=(Button)rlayout1.findViewById(R.id.btnChangeDate);
mDateDisplay=(TextView)rlayout1.findViewById(R.id.tvDate);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
btnDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(getActivity(),
mDateSetListener,
mYear, mMonth, mDay).show();
}
});
return rlayout1;
}
RelativeLayout setLayout2(LayoutInflater inflater)
{
RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
adapterExam=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, subexams);
listview.setAdapter(adapterExam);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedExam=subexams.get(arg2);
total_marks=selectedExam.split("\n")[1].split(" ")[1];
adapter.marks=total_marks;
Cursor mCursor1 = dbAdapter.StudentMarks(selectedExam);
percentages.clear();
if(mCursor1.getCount()>0)
{
getActivity().startManagingCursor(mCursor1);
mCursor1.moveToFirst();
while (mCursor1.isAfterLast() == false) {
String Std_ID = mCursor1.getString(mCursor1.getColumnIndexOrThrow("SID"));
String Std_Prcnt = mCursor1.getString(mCursor1.getColumnIndexOrThrow("Marks"));
percentages.put(Std_ID, Std_Prcnt);
mCursor1.moveToNext();
}
ShowAlertDialog();
}
else
{
vp.setCurrentItem(3);
}
Cursor mCursor = dbAdapter.AllStudents();
getActivity().startManagingCursor(mCursor);
mCursor.moveToFirst();
students.clear();
while (mCursor.isAfterLast() == false) {
String Std_Name = mCursor.getString(mCursor.getColumnIndexOrThrow("Name"));
String Std_ID = mCursor.getString(mCursor.getColumnIndexOrThrow("SID"));
StudentData sd=new StudentData();
sd.name=Std_Name;
sd.id=Std_ID;
sd.percentage=percentages.get(Std_ID);
students.add(sd);
mCursor.moveToNext();
}
adapter.notifyDataSetChanged();
}
});
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
DeleteDialog dd=new DeleteDialog(getActivity(),"SubExams");
dd.Show(subexams, EIDs, adapterExam, arg2);
return true;
}
});
Button btnSave=(Button)rlayout2.findViewById(R.id.btnSave);
btnSave.setText("Add");
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stu
vp.next();
}
});
return rlayout2;
}
void ShowAlertDialog2(final String value)
{
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Are you sure want to save");
//alert.setMessage(msg);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int whichButton) {
// Do something with value!
if(subexams.contains(value))
{
Toast.makeText(getActivity(), "Already Added", Toast.LENGTH_SHORT).show();
return;
}
subexams.add(value);
adapterExam.notifyDataSetChanged();
DBAdapter dbAdapter=new DBAdapter(getActivity());
dbAdapter.insertExams(examTitle, value);
Cursor mCursor1 = dbAdapter.SubExams(examTitle);
getActivity().startManagingCursor(mCursor1);
mCursor1.moveToFirst();
subexams.clear();
EIDs.clear();
while (mCursor1.isAfterLast() == false) {
String Std_Name = mCursor1.getString(mCursor1.getColumnIndexOrThrow("SubTitle"));
String Std_EID = mCursor1.getString(mCursor1.getColumnIndexOrThrow("EID"));
subexams.add(Std_Name);
EIDs.add(Std_EID);
mCursor1.moveToNext();
}
adapterExam.notifyDataSetChanged();
Toast.makeText(getActivity(), "Successfully Saved", Toast.LENGTH_SHORT).show();
vp.previous();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
RelativeLayout setLayout1(LayoutInflater inflater)
{
final RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
MainActivity.setTitle(exams.get(arg2));
examTitle =exams.get(arg2);
Cursor mCursor1 = dbAdapter.SubExams(examTitle);
getActivity().startManagingCursor(mCursor1);
mCursor1.moveToFirst();
subexams.clear();
EIDs.clear();
while (mCursor1.isAfterLast() == false) {
String Std_Name = mCursor1.getString(mCursor1.getColumnIndexOrThrow("SubTitle"));
String Std_EID = mCursor1.getString(mCursor1.getColumnIndexOrThrow("EID"));
subexams.add(Std_Name);
EIDs.add(Std_EID);
mCursor1.moveToNext();
}
adapterExam.notifyDataSetChanged();
vp.next();
}
});
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, exams);
listview.setAdapter(adapter);
DBAdapter dbAdapter=new DBAdapter(getActivity());
Cursor mCursor1 = dbAdapter.Exams();
getActivity().startManagingCursor(mCursor1);
mCursor1.moveToFirst();
exams.clear();
while (mCursor1.isAfterLast() == false) {
String Std_Name = mCursor1.getString(mCursor1.getColumnIndexOrThrow("Title"));
exams.add(Std_Name);
mCursor1.moveToNext();
}
Button btnSave=(Button)rlayout2.findViewById(R.id.btnSave);
btnSave.setText("Add");
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> subjects=new ArrayList<String>();
subjects.add("Option0");
subjects.add("Option1");
subjects.add("Option2");
subjects.add("Option2");
Spinner ExamSpinner = (Spinner)rlayout2.findViewById(R.id.SpinnerExam);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
ArrayAdapter <String> subjectAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_spinner_item, subjects);
subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listview.setAdapter(subjectAdapter);
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch(adapterView.getId()){
case 0:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getActivity().getBaseContext(), "ALEX",Toast.LENGTH_LONG).show();
break;
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
// TODO Auto-generated method stu
/**AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Exam Title");
//alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(getActivity());
alert.setView(input);
alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
exams.add(value);
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();*/
}
});
return rlayout2;
}
RelativeLayout setLayout4(LayoutInflater inflater)
{
RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
adapter=new ListAdapterPercentage(getActivity(), R.layout.listitem_m, R.id.tvAlertTitle, students);
listview.setAdapter(adapter);
Button btnSave=(Button)rlayout2.findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stu
if(isEdit)
{
for(int i=0; i<students.size();i++)
{
try
{
if(students.get(i).percentage.compareTo("")==0)
continue;
dbAdapter.updateMarks(selectedExam, students.get(i).id,
students.get(i).percentage);
}
catch (Exception e) {
// TODO: handle exception
}
}
}
else
{
for(int i=0; i<students.size();i++)
{
try
{
if(students.get(i).percentage.compareTo("")==0)
continue;
dbAdapter.insertMarks(selectedExam, students.get(i).id,
students.get(i).percentage);
}
catch (Exception e) {
// TODO: handle exception
}
}
}
vp.setCurrentItem(1);
Toast.makeText(getActivity(), "Successfully Saved", Toast.LENGTH_SHORT).show();
}
});
return rlayout2;
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
void ShowAlertDialog()
{
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Already Added");
alert.setMessage("Do you want to");
alert.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do something with value!
isEdit=true;
vp.setCurrentItem(3);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}
答案 0 :(得分:0)
我找到答案,创建听众我必须做以下事情:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getActivity().getBaseContext(), "0",Toast.LENGTH_LONG).show();
}
}
});