2

I have a small application that retrieves data from a server. This is done with an function i've build. This function returns a List<string>.

Now the issue that i am having. I want to insert the list that i retrieve from my function into a ListView.

I'm using the code below to try this:

List<string> range = _connection.GetData("animals");
ArrayAdapter adapter = new ArrayAdapter<String>(this,Resource.Layout.dataView, range);
listView.SetAdapter (adapter);

Range is the list that i get from my function GetData() Now i want to insert the data from the range list into my listview but as soon as i start the application en press the button to do this the application shuts down with this message The application has stopped unexpectedly. Please try again.. Can someone tell me what i am doeing wrong here or show me how i can insert the data into my listbox.

Edit

I checked the ouput of the application and it says the following:

[AndroidRuntime] java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

So i know why the application shuts down. Can someone show me or tell me how i have to insert the data because the adapter requires an Textview to work.

Edit

This is my XML for my listview

<ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/dataListView" />

Thanks in advance

Final Edit 10/9/2014

After a lot of research en being pointed in the right direction i finnaly found out what i was doeing wrong.

This is how you insert data into an listview:

ArrayAdapter adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1, response);
            listView.Adapter = adapter;

The problem was that i used an outdated function SetAdapter() To everyone in the comments/ awnsers thanks for the help.

1 Answers1

2

Resource.Layout.dataView must be the id of a xml layout file containing only a TextView.

if it contains a Layout the use :

ArrayAdapter<String>(this, R.layout.a_layout_file, R.id.a_text_view_within_layout, this.file)

or else use:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    // other attributes of the TextView
/>
Sagar Pilkhwal
  • 5,914
  • 2
  • 27
  • 78
  • Thanks for the help, but like i said i want to use a listvieuw. is there a way to do this or do i have to use a textview –  Sep 09 '14 at 13:44
  • you need to write a custom adapter for that – Metehan Sep 09 '14 at 13:47
  • I want to use a Listview yes –  Sep 09 '14 at 13:49
  • look into this [article](http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_1_-_listview_parts_and_functionality/) it gives in depth knowledge about list view in xamrin – Sagar Pilkhwal Sep 09 '14 at 13:49
  • I've got a small question regarding this page: http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_2_-_populating_a_listview_with_data/ There is a ListAdapapter in the code there but i cant find that –  Sep 09 '14 at 14:31
  • `ListAdapter = new ArrayAdapter`, here `ListAdapter` is an object – Sagar Pilkhwal Sep 09 '14 at 15:31
  • ah oke so i have to make a Obj ListAdapter –  Sep 10 '14 at 06:39
  • Thx i will try it out, i will accept as soon as possible –  Sep 10 '14 at 06:45