bb13
bb13

Reputation: 9

displaying an ArrayList in ListView Android

I am having difficulty displaying data stored by button clicks that are saved in an SqlLite database into a ListView. It doesn't show the data as i want underneath the button when i click it to add the data to the database.

My code:

boolean hasMoreData = cursor.moveToFirst();
    while (hasMoreData) {
        // get the value out of each column
        long key = cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry._ID));
        String studentID = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
        String studentGrade = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));
        //For now, just print out the record to the log.      

        ArrayList<String> myList = new ArrayList<>();
        myList.add( " student id: "+ studentID);
        myList.add(" student grade : " +studentGrade);
        ArrayAdapter<String> myAdapter =
                new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,myList);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(myAdapter);


        System.out.println("RECORD KEY: " + key + " student id: " + studentID + " student grade : " + studentGrade);




        //move on to the next row!
        hasMoreData = cursor.moveToNext();
    }

what this looks like in emulator :

current display

What I want:

what i want it to look like

Upvotes: 0

Views: 1105

Answers (2)

Ihdina
Ihdina

Reputation: 1760

Place a ArrayAdapter and ListView in outside the loop.

ArrayList<String> myList = new ArrayList<>();

boolean hasMoreData = cursor.moveToFirst();
while (hasMoreData) {
    long key = cursor.getLong(cursor.getColumnIndexOrThrow(MyDataEntry._ID));
    String studentID = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_ID_COLUMN));
    String studentGrade = cursor.getString(cursor.getColumnIndexOrThrow(MyDataEntry.STUDENT_GRADE_COLUMN));

    myList.add( " student id: "+ studentID);
    myList.add(" student grade : " +studentGrade);

    hasMoreData = cursor.moveToNext();
}   
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, myList);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myAdapter);

Upvotes: 1

Shmuel
Shmuel

Reputation: 3916

Every time you call ArrayList<String> myList = new ArrayList<>(); and ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(); you are resetting the list.

Remove ArrayList<String> myList = new ArrayList<>();

and move ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1); ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(myAdapter);

above your loop and you'll fix this problem

inside the loop call 'myAdapter.add("whatever you want");' and then myAdapter.notifyDataSetChanged() to update the ui

Upvotes: 0

Related Questions