【发布时间】:2013-07-26 15:14:07
【问题描述】:
我正在使用TextViews 或ImageViews 构建一个ListView,具体取决于该项目是否是文本项目或是否具有与之关联的图像。我使用了Customizing list shown from SimpleCursorAdapter using VewBinder,效果很好,在一定程度上。
我有一个SQLite database,其中包含所有具有标题(一些文本)的项目,但并非所有项目都具有图像/视频作为资源;所以有些是纯文本项目,而有些是图像或视频。对于图片和视频我想加载一个资源,但是当项目是纯文本项目时,我只想显示图片的标题。
所以我的XML 包含两个元素——TextView 用于显示文本项的标题,ImageView 用于显示资源文件(对于视频,我检索视频 ID 的 YouTube 默认图像)。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp"
android:background="@android:color/black"
>
<TextView
android:id="@+id/tv_details_title"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@android:color/white"
android:visibility="visible"
android:background="@android:color/holo_blue_bright" />
<ImageView
android:id="@+id/iv_details_resource"
android:layout_width="200dp"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_gravity="center"
android:src="@drawable/logo"
android:visibility="visible"
android:background="@android:color/holo_green_light" />
</LinearLayout>
我没有使用Content Provider,因为我不需要其他应用程序来使用我的数据库。我使用SimpleCursorAdapter 并编写了CustomViewBinder(内部)类,根据是否有与该项目关联的资源将我的数据绑定到我的views。
问题是ImageView 的可见性总是设置为GONE 无论如何。所以显示每个项目的标题,因为它总是绑定到TextView元素,但是当我的数据库中没有列出资源时(即“null”在resource字段的记录中),然后ImageView 也消失了。
这是我的CustomViewBinder的代码:
private class CustomViewBinder implements ViewBinder{
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(columnIndex == cursor.getColumnIndex(DatabaseHelper.FIELD_RESOURCE)){
Log.d("CustomViewBinder", "columnIndex = " + columnIndex);
//If the column is resource, ten we use custom view.
String resource = cursor.getString(columnIndex);
Log.d("CustomViewBinder", "resource = " + resource);
if(resource.equalsIgnoreCase("null")){
Log.d("CustomViewBinder", "Inside if resource = " + resource);
Log.d("CustomViewBinder", "Set the image view to GONE");
view.setVisibility(View.GONE);
}else{
Log.d("CustomViewBinder", "Inside else resource = " + resource);
columnIndex = cursor.getColumnIndex(DatabaseHelper.FIELD_TITLE);
Log.d("CustomViewBinder", "columnIndex = " + columnIndex);
//If the column is resource, ten we use custom view.
String title = cursor.getString(columnIndex);
Log.d("CustomViewBinder", "title = " + title);
if(!resource.equalsIgnoreCase("null")){
Log.d("CustomViewBinder", "Set the title view to GONE");
view.setVisibility(View.GONE);
}
return true;
}
return true;
}
return false;
}
为了清楚起见,还有一些日志:
07-26 17:05:36.343: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:36.343: D/CustomViewBinder(9735): resource = null
07-26 17:05:36.343: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:36.343: D/CustomViewBinder(9735): Set the image view to GONE
07-26 17:05:36.353: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:36.353: D/CustomViewBinder(9735): resource = null
07-26 17:05:36.353: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:36.353: D/CustomViewBinder(9735): Set the image view to GONE
07-26 17:05:36.363: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:36.363: D/CustomViewBinder(9735): resource = null
07-26 17:05:36.363: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:36.363: D/CustomViewBinder(9735): Set the image view to GONE
07-26 17:05:53.770: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:53.770: D/CustomViewBinder(9735): resource = Notes_Box_2_Notebook_1_006.jpg
07-26 17:05:53.770: D/CustomViewBinder(9735): Inside else resource = Notes_Box_2_Notebook_1_006.jpg
07-26 17:05:53.770: D/CustomViewBinder(9735): columnIndex = 2
07-26 17:05:53.770: D/CustomViewBinder(9735): title = Notebook page - man and wife
07-26 17:05:53.770: D/CustomViewBinder(9735): Set the title view to GONE
07-26 17:05:54.310: D/CustomViewBinder(9735): columnIndex = 4
07-26 17:05:54.310: D/CustomViewBinder(9735): resource = null
07-26 17:05:54.310: D/CustomViewBinder(9735): Inside if resource = null
07-26 17:05:54.310: D/CustomViewBinder(9735): Set the image view to GONE
问题是在“Set the title view to GONE”处,“image view”仍设置为GONE,因此图像“Notes_Box_2_Notebook_1_006.jpg”没有显示——它的标题是。
这是因为CustomViewBinder 中的“view”似乎总是我的XML 中的ImageView,而不是TextView。
如何在CustomViewBinder 中访问“另一个”view?
【问题讨论】:
-
marienke - 你得到答案了吗?
标签: android android-listview customization simplecursoradapter