【问题标题】:Error: ArrayAdapter requires the resource ID to be a TextView错误:ArrayAdapter 要求资源 ID 为 TextView
【发布时间】:2011-10-12 22:50:31
【问题描述】:

这是什么意思?我一直在与我使用ArrayAdapterListView 作斗争。我写了这个很好的对话框片段,我已经回调了一个 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>

那么我有:

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

然后出现上述错误,所以我将其切换为仅使用

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();        
}

这在我的更新方法中没有出错(更新用户界面),但它没有更新任何东西。所以我不知道如何更新这个 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> 

【问题讨论】:

  • 您能发布您的 R.layout.location_row XML 文件吗?
  • 完成,我意识到错误是声称它无法将 TextView 设置为其他内容(可能是我的位置类型 complexClass),尽管我怀疑覆盖 toString 会解决这个问题?

标签: android android-listview android-arrayadapter


【解决方案1】:

正如我们在http://developer.android.com/reference/android/widget/ArrayAdapter.html 中看到的那样 ArrayAdapter 仅适用于文本视图上的字符串。你可以使用这个构造函数

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

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

getView(int, View, ViewGroup) 

返回自定义类型的视图。

【讨论】:

  • 那么真的很愚蠢的问题,我在哪里覆盖getView?我必须扩展 ArrayAdapter 吗?
  • 我认为通常扩展 BaseAdapter 类
  • 这基本上是在创建一个CustomAdapter吗?
  • 是的,扩展 BaseAdapter 的自定义适配器。在 getView() 上,您可以扩展布局并将其返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
  • 2012-03-06
  • 1970-01-01
  • 2015-10-15
相关资源
最近更新 更多