0

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

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

putting ArrayList<String> myList; above the while loop gives me error in the new ArrayAdapter call for myList not being initalized
nvm i fixed it by calling new
this only shows the last grade added it seems for some reason though
putting ArrayList on top of the loop.
0

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

4 Comments

Outside of my loop where exactly? above or below the entry to the while?
Right above the while loop
ok, that works for getting all the data. Now my question is, how do i show the data currently added from the button click?
updated my answer. but EYN's answer is much cleaner ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.