lundi 7 mars 2016

How to create a listView from string array

Hi I am trying to get data from a SQLite database and add it into a listview so far I have the following however it is not working. I am not sure if the data is being added into the array correctly. I have a function called ViewAll which is called within onCreate.

I have this within a class called datahelper

    public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " +TABLE_NAME, null);
    return res;


}

and then the following class which is supposed to display the data within a listview

   public void ViewAll() {
    Cursor res = myDb.getAllData();
    String[] data;
    if (res != null) {
        while (res.moveToNext()) {
            data = new String[10];
            data[0] = String.valueOf(res.getInt(0));
            data[1] = res.getString(1);
            data[2] = res.getString(2);
            data[3] = String.valueOf(res.getInt(3));
            data[4] = String.valueOf(res.getFloat(4));
            data[5] = String.valueOf(res.getFloat(5));
            data[6] = String.valueOf(res.getInt(6));
            data[7] = String.valueOf(res.getInt(7));
            data[8] = String.valueOf(res.getInt(8));
            data[9] = String.valueOf(res.getInt(9));

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.display_data_layout, data);

            ListView list = (ListView) findViewById(R.id.displayview);
            list.setAdapter(adapter);

        }

    }
}

Aucun commentaire:

Enregistrer un commentaire