使用ViewBinder自定义SimpleCursorAdapter中显示的列表

时间:2013-07-26 15:14:07

标签: android android-listview customization simplecursoradapter

我正在使用ListViewTextView来构建ImageView,具体取决于该项是文本项还是与之关联的图像。我使用Customizing list shown from SimpleCursorAdapter using VewBinder并且它很有效,直到某一点。

我有SQLite database个包含所有标题(一些文字)的项目,但并非所有项目都有图像/视频作为资源;所以有些是纯文本项目,有些则是图像或视频。对于我想要加载资源的图像和视频,但当项目是纯文本项目时,我只想显示图像的标题。

因此我的XML包含两个元素 - 一个TextView用于显示文本项的标题,一个ImageView用于显示资源文件(对于视频我检索YouTube默认图像对于视频ID)。

<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(内部)类,根据是否有与该项关联的资源将我的数据绑定到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。< / p>

如何访问view中的“其他”CustomViewBinder

1 个答案:

答案 0 :(得分:0)

我使用了ViewHolder模式。

我通过Cursor扩展类来实现它。在我的主要活动中,我从我的数据库助手类中获取光标,该类查询数据库。然后,我将CustomCursorAdapterhorizontal listview关联,其中包含三个项目 - TextView和两个ImageView。然后我将OnItemClickListener设置为水平listview。然后,您可以对某些内容进行过滤,但我刚刚更新了水平view上方的主listview(与链接教程非常相似)。

在我的CustomCursorAdapter的{​​{1}}函数中,我定义了一个getView,将光标移动到位置(来自getView的参数),并且几乎在每个示例中都执行了ViewHolder函数,所以这里是:

getView