Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Listview and ArrayAdapter

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);
                                mdr = command.ExecuteReader();

                                mItems = new List<string>();

                                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?


  • 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();
    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.