1. Download our Official Android App: Forums for Android!

Apps Getting sqlite data into expandableList

Discussion in 'Android Development' started by Pooveshin, Sep 6, 2016.

  1. Pooveshin

    Pooveshin Member
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    32
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    32
    3
    26
    Male
    Managing Partner
    Durban, South Africa
    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;
    }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,844
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,844
    11,445
    1,988
    Male
    Software developer
    South West of England
    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 (Text):
    1.  
    2. Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null);
    3. resultSet.moveToFirst();
    4. String username = resultSet.getString(1);
    5. String password = resultSet.getString(2);
    6.  
    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
     
  3. Pooveshin

    Pooveshin Member
    Thread Starter
    Rank:
    None
    Points:
    26
    Posts:
    32
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    32
    3
    26
    Male
    Managing Partner
    Durban, South Africa
    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));
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,844
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,844
    11,445
    1,988
    Male
    Software developer
    South West of England
    Are you sure the cursor is at the start of your result set? If not, use the following pattern to iterate:

    Code (Text):
    1.  
    2. if (res.moveToFirst())
    3. {
    4.    do
    5.    {
    6.         // do what you need with the cursor here
    7.     } while (res.moveToNext());
    8. }
    9.  
    Also check the value returned from res.getCount(). That tells you how many records are in your result set.
     

Share This Page

Loading...