【发布时间】:2011-10-12 22:50:31
【问题描述】:
这是什么意思?我一直在与我使用ArrayAdapter 的ListView 作斗争。我写了这个很好的对话框片段,我已经回调了一个 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