0

我正在构建一个ListView带有TextViews 或ImageViews 的项目,具体取决于该项目是文本项目还是具有与之关联的图像。我使用了使用 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

我如何访问“另一个viewCustomViewBinder

4

1 回答 1

0

我使用了ViewHolder模式。

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

在我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-08-28T09:58:40.273 回答