Home » Android » android – Error: ArrayAdapter requires the resource ID to be a TextView

android – Error: ArrayAdapter requires the resource ID to be a TextView

Posted by: admin June 15, 2020 Leave a comment

Questions:

What does this mean? I have been fighting with this ListView that I have using an ArrayAdapter. I wrote this nice dialog fragment that I have calling back to an updateUI listner cause I want to update this ListView I have in my fragment from the DialogFragment at first the ArrayAdapter was a complex type of a class i Created:

ArrayAdapter<Location> theLocations;
...
//in oncreateview
theLocations = new ArrayAdapter<Location>(mContext, R.layout.location_row, locations);
//here locations is an ArrayList<Location>

then I have:

public void onUIUpdate(Location l) { //called from dialog fragment      
 locations.add(l);                  
 theLocations.notifyDataSetChanged();       
}

then that gave the above error so I switched it to use just a

String[] locationNames = new String[sizeofLocations];
theLocations=new ArrayAdapter<String>(mContext, R.layout.location_Row, locationNames );

public void onUIUpdate(Location l) {        
    locations.add(l);                   
            locationNames = new String[locations.size()];
            for(locations.size() etc...) { 
               locationNames [i] = new String(locations.get(i).getName());
            }
    theLocations.notifyDataSetChanged();        
}

this didn’t error out in my update method (That updates the ui) but it didn’t update anything. So I am lost as to how to update this ArrayAdapter, I thought notifyChange was supposed to do it, but it either does nothing or throws the above error.

I have no issues with my SimpleCursorAdapters elsewhere in my program (As long as my cursors remain open and i call a requery on them).

Any insights into what I am doing wrong?

Upon request here is my layout for the R.layout.location_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"     android:orientation="horizontal">   
<TextView android:text="@+id/TextView01" android:layout_height="wrap_content" android:id="@+id/location_name"
    android:textSize="15px" android:layout_marginTop="10px" android:layout_width="wrap_content"></TextView>
<TextView android:text="|" android:layout_height="wrap_content" 
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>
<TextView android:text="@+id/TextView01" android:layout_height="wrap_content" android:id="@+id/location_lat"
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>
<TextView android:text="|$" android:layout_height="wrap_content" 
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>  
<TextView android:text="@+id/TextView01" android:layout_height="wrap_content" android:id="@+id/location_lon"
    android:textSize="15px" android:layout_marginTop="6px" android:layout_width="wrap_content"></TextView>
</LinearLayout> 
How to&Answers:

As we can read in http://developer.android.com/reference/android/widget/ArrayAdapter.html
ArrayAdapter just works with strings on textviews. You can use this constructor

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

specifyng the id of a TextView inside your layout or override

getView(int, View, ViewGroup) 

returning a view of your custom type.