我正在构建一个ListView
带有TextView
s 或ImageView
s 的项目,具体取决于该项目是文本项目还是具有与之关联的图像。我使用了使用 VewBinder 从 SimpleCursorAdapter 显示的自定义列表,它在一定程度上工作得很好。
我有一个SQLite database
包含项目,它们都有一个标题(一些文本),但并非所有项目都有图像/视频作为资源;所以有些是纯文本项目,而有些是图像或视频。对于图像和视频,我想加载资源,但是当项目是纯文本项目时,我只想显示图像的标题。
所以 myXML
包含两个元素 - 一个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>
我没有使用 aContent Provider
因为我不需要其他应用程序来使用我的数据库。我使用 aSimpleCursorAdapter
并编写了一个(内部)类,根据是否有与该项目关联的资源CustomViewBinder
将我的数据绑定到我的 s 。view
问题是,无论发生什么,它的可见性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
”没有显示——它的标题是。
这是因为“ view
”的CustomViewBinder
似乎总是ImageView
在我的XML
而不是曾经的TextView
。
我如何访问“另一个view
” CustomViewBinder
?