• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting sqlite data into expandableList

Pooveshin

Newbie
Hi All

I need to show data from a table in a sqlite database in an expandablelistview, can anyone help?
I have static data for now until i can get the data from the DB. The child data needs to come from the DB.

private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// Adding child data
listDataHeader.add("Accident Number : 1");
listDataHeader.add("Accident Number : 2");
listDataHeader.add("Accident Number : 3");

// Adding child data
List<String> AccYV = new ArrayList<String>();
AccYV.add("Accident Number : ");
AccYV.add("Registration Number : ");
AccYV.add("Make & Model");
AccYV.add("Address of Owner");
AccYV.add("Name of Driver");
AccYV.add("Address of Driver");
AccYV.add("Tel no.Driver");

public Cursor getYVAllData()
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor res = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return res;
}
 
The answer remains the same as in your previous duplicate thread. You need to examine your result set cursor, extracting the required data fields from it.

But as it stands, nobody can give a specific answer to you because we don't know the structure of your database table.

As explained on the web page (url below), you need to use the cursor to extract fields e.g.

Code:
Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null);
resultSet.moveToFirst();
String username = resultSet.getString(1);
String password = resultSet.getString(2);

As you can see, the code is getting specific fields from the result set '1' and '2'.

http://www.tutorialspoint.com/android/android_sqlite_database.htm
 
Thank you, I have it working where the data is writing to the ExpandableListView however my new problem is, it is only showing the last record in the table. Can you please take a look at what I have done and how I can modify for multiple lines to show.

private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// Adding child data
listDataHeader.add("Accident Number : 1");
listDataHeader.add("Accident Number : 2");
listDataHeader.add("Accident Number : 3");

Cursor res = DB.getYVAllData();
res.getCount();
while (res.moveToNext())
{
// Adding child data
List<String> AccYV = new ArrayList<String>();
AccYV.add("Accident Number : " + res.getString(0));
AccYV.add("Registration Number : " + res.getString(1));
AccYV.add("Make & Model : " + res.getString(2));
AccYV.add("Name of Owner : " + res.getString(3));
AccYV.add("Address of Owner : " + res.getString(4));
AccYV.add("Name of Driver : " + res.getString(5));
AccYV.add("Address of Driver : " + res.getString(6));
AccYV.add("Tel no.Owner : " + res.getString(7));
AccYV.add("Tel no.Driver : " + res.getString(8));
AccYV.add("Vehicle Insured : " + res.getString(9));

List<String> AccOV = new ArrayList<String>();
AccOV.add("Accident Number : " + res.getString(0));
AccOV.add("Registration Number : " + res.getString(1));
AccOV.add("Make & Model : " + res.getString(2));
AccOV.add("Name of Owner : " + res.getString(3));
AccOV.add("Address of Owner : " + res.getString(4));
AccOV.add("Name of Driver : " + res.getString(5));
AccOV.add("Address of Driver : " + res.getString(6));
AccOV.add("Tel no.Owner : " + res.getString(7));
AccOV.add("Tel no.Driver : " + res.getString(8));
AccOV.add("Vehicle Insured : " + res.getString(9));
 
Are you sure the cursor is at the start of your result set? If not, use the following pattern to iterate:

Code:
if (res.moveToFirst()) 
{
   do 
   {
        // do what you need with the cursor here
    } while (res.moveToNext());
}

Also check the value returned from res.getCount(). That tells you how many records are in your result set.
 
Back
Top Bottom