我想用我从数据库中保存的所有数据填充我的 Listview,但是我无法检索所有数据,而是只获得第一条记录和记录本身的计数。
MainActivity.java
//Populating the ListView
ArrayList<HashMap<String, String>> mylistData = new ArrayList<HashMap<String, String>>();
int[] columnIds = new int[] {R.id.tv_Lastname, R.id.tv_Firstname, R.id.tv_MI, R.id.tv_Birthdate, R.id.tv_HomeNo,
R.id.tv_Mobile, R.id.tv_email, R.id.tv_Brgy};
ArrayList<String> arrayPatientData = db.arrayListPatientData();
Cursor cursor = db.getPatientData();
for(int i=0; i<cursor.getCount(); i++) {
HashMap<String,String> map = new HashMap<String, String>();
for(int j=0; j<arrayPatientData.size(); j++) {
Cursor cursorPatientData = db.getPatientData(j);
//Cursor cursorCityData = db.getCityData();
//Cursor cursorProvinceData = db.getProvinceData();
firstname = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_FNAME));
lastname = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_LNAME));
middlename = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_MNAME));
bday = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_BIRTHDAY));
homeNo = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_HOME_NO));
mobileNo = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_MOB_NO));
email = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_EMAIL));
brgy = cursorPatientData.getString(cursorPatientData.getColumnIndex(Constants.PATIENT_BRGY));
//city = cursorCityData.getString(cursorCityData.getColumnIndex(Constants.CITY_ID));
//province = cursorProvinceData.getString(cursorProvinceData.getColumnIndex(Constants.PROVINCE_ID));
map.put(FIRSTNAME, firstname);
map.put(LASTNAME, lastname);
map.put(MIDDLENAME, middlename);
map.put(BDAY, bday);
map.put(HOMENO, homeNo);
map.put(MOBILENO, mobileNo);
map.put(EMAIL, email);
map.put(BRGY, brgy);
//map.put(CITY, city);
//map.put(PROVINCE, province);
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter = new SimpleAdapter(getApplicationContext(), mylistData, R.layout.attribute_patient,
new String[] {FIRSTNAME, LASTNAME, MIDDLENAME, BDAY, HOMENO, MOBILENO, EMAIL, BRGY} , columnIds);
lv_Patient.setAdapter(arrayAdapter);
数据库处理程序.java
public Cursor getPatientData(int _id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(Constants.TABLE_PATIENT, new String[] {
Constants.PATIENT_ID, Constants.PATIENT_FNAME, Constants.PATIENT_LNAME,
Constants.PATIENT_MNAME, Constants.PATIENT_BIRTHDAY, Constants.PATIENT_HOME_NO,
Constants.PATIENT_MOB_NO, Constants.PATIENT_EMAIL, Constants.PATIENT_BRGY,
Constants.F_PATIENT_CITY_ID, Constants.F_PATIENT_PROVINCE_ID}, Constants.PATIENT_ID + "=?",
new String[] {String.valueOf(_id)}, null, null, null);
if (c != null) {
c.moveToFirst();
}
db.close();
return c;
}
//Get User Data
public Cursor getPatientData()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(Constants.TABLE_PATIENT, new String[] {
Constants.PATIENT_ID, Constants.PATIENT_FNAME, Constants.PATIENT_LNAME,
Constants.PATIENT_MNAME, Constants.PATIENT_BIRTHDAY, Constants.PATIENT_HOME_NO,
Constants.PATIENT_MOB_NO, Constants.PATIENT_EMAIL, Constants.PATIENT_BRGY,
Constants.F_PATIENT_CITY_ID, Constants.F_PATIENT_PROVINCE_ID}, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
db.close();
return c;
}
日志猫:
07-24 18:31:29.585: E/AndroidRuntime(14671): FATAL EXCEPTION: main
07-24 18:31:29.585: E/AndroidRuntime(14671): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.palsproject/com.example.palsproject.S_5th_Search_Patient}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.os.Looper.loop(Looper.java:137)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.ActivityThread.main(ActivityThread.java:4514)
07-24 18:31:29.585: E/AndroidRuntime(14671): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 18:31:29.585: E/AndroidRuntime(14671): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 18:31:29.585: E/AndroidRuntime(14671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-24 18:31:29.585: E/AndroidRuntime(14671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-24 18:31:29.585: E/AndroidRuntime(14671): at dalvik.system.NativeStart.main(Native Method)
07-24 18:31:29.585: E/AndroidRuntime(14671): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
07-24 18:31:29.585: E/AndroidRuntime(14671): at com.example.palsproject.S_5th_Search_Patient.onCreate(S_5th_Search_Patient.java:66)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.Activity.performCreate(Activity.java:4465)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-24 18:31:29.585: E/AndroidRuntime(14671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-24 18:31:29.585: E/AndroidRuntime(14671): ... 11 more