【问题标题】:Customizing list shown from SimpleCursorAdapter using ViewBinder使用 ViewBinder 从 SimpleCursorAdapter 显示的自定义列表
【发布时间】: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


【解决方案1】:

我使用了ViewHolder 模式。

我通过一个 Cursor 扩展类来实现它。在我的主要活动中,我从查询数据库的数据库助手类中获取光标。然后我将我的CustomCursorAdapter 链接到一个horizontal listview,它包含三个项目——一个TextView 和两个ImageViews。然后我将OnItemClickListener 设置为我的水平listview。然后你可以过滤一些东西,但我只是在我的水平 listview 上方更新了一个主 view(很像链接教程中的)。

在我的CustomCursorAdaptergetView 函数中,我定义了一个ViewHolder,将光标移动到该位置(来自getView 的参数),然后执行几乎每个getView 函数示例中所做的操作,所以这里是:

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder = null;
    cursor.moveToPosition(position);

    if(convertView == null){
        convertView = inflater.inflate(R.layout.activity_media_items, null);            
        holder = new ViewHolder();

        holder.textview = (TextView) convertView.findViewById(R.id.tv_details_title);
        holder.imageview = (ImageView) convertView.findViewById(R.id.iv_details_resource_image);
        holder.imageviewvideo = (ImageView) convertView.findViewById(R.id.iv_details_resource_video);

        convertView.setTag(holder);         
    }else{
        holder = (ViewHolder) convertView.getTag();
    }               

    //////////////////////////TYPE      
    type = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_ITEM_TYPE));
    itemId = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_MEDIA_ITEM_ID));

    if(type.equalsIgnoreCase("text")){

        //////////////////////////TEXT
        String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_TRANSCRIPTION));
        title = title.subSequence(0, 150) + "...";
        holder.textview.setText(title);

        holder.textview.setVisibility(View.VISIBLE);
        holder.imageview.setVisibility(View.GONE);
        holder.imageviewvideo.setVisibility(View.GONE); 
    }

    if(type.equalsIgnoreCase("image")){

        //////////////////////////IMAGE         
        String imageUri = "assets://";
        Log.d(TAG, "URI = " + imageUri + fileName);     

        ImageLoader.getInstance().displayImage(imageUri+fileName, holder.imageview);
        holder.textview.setVisibility(View.GONE);
        holder.imageview.setVisibility(View.VISIBLE);
        holder.imageviewvideo.setVisibility(View.GONE);         
    }

    if(type.equalsIgnoreCase("video")){

        //////////////////////////VIDEO 
        //get the file name
        String fileName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_RESOURCE));

        //get the image of a youtube video because it's a video                                 
        try {
            Log.d(TAG,"Want to show video at -> http://img.youtube.com/vi/"+fileName+"/mqdefault.jpg");
            ImageLoader.getInstance().displayImage("http://img.youtube.com/vi/"+fileName+"/mqdefault.jpg", holder.imageviewvideo);
        } catch(Exception e) {
            Log.d("YouTube photo", "Failed to load photo!!! "+e.toString());
        }

        holder.textview.setVisibility(View.GONE);
        holder.imageview.setVisibility(View.GONE);
        holder.imageviewvideo.setVisibility(View.VISIBLE);
    }       
    return convertView;     
  }

【讨论】:

    猜你喜欢
    • 2013-04-14
    • 2012-04-14
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多