Listview and ArrayAdapter

Hello,
I'm trying to use pull data from MySQL to a list view but the code I have built is only displaying the very last entry within MySQL.

if I have 25 rows in the MySQL database and I want to display data from the rows that have 'soothe' within the rows, the code will only display row 25.
row 2, 8, 13, and 25 have the word 'soothe' within them. I want to display all 4 of those rows but my listview only displays one Item within the listview.

The code I have been working with.

                           MySqlDataReader mdr;
                                command = new MySqlCommand(selectFromDB, con);
                                con.Open();
                                mdr = command.ExecuteReader();
                                //mdr.Read();


                           while(mdr.Read())
                            {
                                mItems = new List<string>();
                                mItems.Add(mdr.GetString("soothe"));

                                ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mItems);

                                mlistview.Adapter = adapter;                       
                            }

I think this has something to do with the UI not updating. I have looked around the internet but I'm stuck. can someone please point me in the correct direction?

Posts

  • cwphillicwphilli USMember ✭✭✭
    edited May 2017

    Why are you creating a new adapter inside of your while(mdr.Read()) loop?
    Move the unnecessary lines outside the loop like so:

    mItems = new List();
    while(mdr.Read())
    mItems.Add(mdr.GetString("soothe"));
    var adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, mItems);
    mlistview.Adapter = adapter;

  • BlueScreenThisBlueScreenThis USMember ✭✭

    Hello cwphilli,
    Thanks for the reply.

    I ended up figuring this out by removing the "mItems = new List<strings();>" outside of the while loop.
    I originally had the "new array adapter" below the "while loop" but i was trying different things to resolve my issue.
    thought i had to use NotifyDataSetChanged() to apply additional rows to the list view.

Sign In or Register to comment.