错误:ArrayAdapter要求资源ID为TextView

Cod*_*joy 8 android android-arrayadapter android-listview

这是什么意思?我一直在与之斗争ListView,我使用过ArrayAdapter.我写了这个很好的对话框片段,我已经调用了一个updateUI列表器,因为我想要更新这个ListView我在我的片段DialogFragment中起初ArrayAdapter是一个复杂类型的我创建的:

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

然后我有:

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

然后,这给出了上述错误,所以我切换它只使用一个

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();        
}
Run Code Online (Sandbox Code Playgroud)

这在我的更新方法中没有错误(更新了ui)但它没有更新任何东西.所以我迷失了如何更新这个ArrayAdapter,我认为notifyChange应该这样做,但它要么什么都不做,要么抛出上面的错误.

我的程序中的其他地方的SimpleCursorAdapters没有问题(只要我的游标保持打开状态,我就会对它们进行重新查询).

我对错误的看法有何见解?

根据要求,这里是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> 
Run Code Online (Sandbox Code Playgroud)

Raf*_*esp 19

正如我们可以在http://developer.android.com/reference/android/widget/ArrayAdapter.html中看到的, ArrayAdapter只适用于textviews上的字符串.您可以使用此构造函数

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Run Code Online (Sandbox Code Playgroud)

在布局或覆盖中指定TextView的id

getView(int, View, ViewGroup) 
Run Code Online (Sandbox Code Playgroud)

返回自定义类型的视图.

  • 是的,一个扩展BaseAdapter的自定义适配器.在getView()上,您可以给布局充气并返回它. (3认同)