Hey guys I've been trying to work on an android project for my college but I am stuck at this point. I want to fetch all the rows from the table students
, populate them into the ArrayList
and then use ListView
or CardView
to display each student's record. This is the code I'm using:
public ArrayList<String> getStudentsEntry()
{
ArrayList<String> arrayList = new ArrayList<>();
Cursor cursor = db.query("STUDENT", ALL_COLUMN_NAME, null, null, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return null;
}
cursor.moveToFirst();
while(cursor.isAfterLast()) {
String name = cursor.getString(cursor.getColumnIndex("NAME"));
String enrollment = cursor.getString(cursor.getColumnIndex("ENROLLMENT"));
String year = cursor.getString(cursor.getColumnIndex("YEAR"));
String dob = cursor.getString(cursor.getColumnIndex("DOB"));
String email = cursor.getString(cursor.getColumnIndex("EMAIL"));
String caste = cursor.getString(cursor.getColumnIndex("CASTE"));
String fname = cursor.getString(cursor.getColumnIndex("FNAME"));
String mname = cursor.getString(cursor.getColumnIndex("MNAME"));
String address = cursor.getString(cursor.getColumnIndex("ADDRESS"));
String contact = cursor.getString(cursor.getColumnIndex("CONTACT"));
arrayList.add(name);
arrayList.add(enrollment);
arrayList.add(year);
arrayList.add(dob);
arrayList.add(email);
arrayList.add(caste);
arrayList.add(fname);
arrayList.add(mname);
arrayList.add(address);
arrayList.add(contact);
}
return arrayList;
}
It's not returning anything.I'm not able to fetch the data from the database. The array size is zero:
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:25
Please help me out guys. I'm new to this stuff.