为什么setImageResource显示什么? [英] Why setImageResource displays nothing?

查看:201
本文介绍了为什么setImageResource显示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这取决于数据库的价值在我的ListView中显示的图标。我按照<一href=\"http://stackoverflow.com/questions/2192082/android-changing-an-imageview-src-depending-on-database-field-data/2193727#2193727\">this回答这样做。
但结果显示任何内容。以下是我在我的 row.xml

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT
     机器人:方向=横向&GT;    &LT; ImageView的
        机器人:ID =@ + ID / movie_su​​bscribed_icon
        机器人:填充=3dip
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@绘制/ star_off/&GT;    &LT;的LinearLayout
         机器人:layout_width =FILL_PARENT
         机器人:layout_height =WRAP_CONTENT
         机器人:方向=垂直&GT;     &LT; TextView的机器人:ID =@ + ID / MOVIE_NAME
     ...

和这里是code:

  movies.setViewBinder(新SimpleCursorAdapter.ViewBinder(){        @覆盖
        公共布尔setViewValue(查看视图,光标光标,诠释参数:columnIndex){
          INT viewId = view.getId();
          开关(viewId){
          案例R.id.movi​​e_name:
                  INT readValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
                  如果(readValue == 1){//观看电影项
                      TextView中的movieName =(TextView的)视图。
                      movieName.setTextColor(Color.GRAY);
                  }
                  打破;
          案例R.id.movi​​e_su​​bscribed_icon:
              INT subscribedValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
                  如果(subscribedValue大于0){//订阅电影项
                      ImageView的movieIcon =(ImageView的)视图。
                      movieIcon.setImageResource(R.drawable.star_off);
                  }
              打破;
          }
          返回false;
        }      });

我特别使用了相同的图标在我的code作为默认值。
什么是错在这里? (我有 star_off 绘制,华电国际绘制-MDPI 文件夹只)

UPD 。以下code效果很好:

  movieIcon.setImageDrawable(getResources()getDrawable(R.drawable.star_off));


解决方案

我已经找到了另外一个问题的解决方案。


  

基本上setViewValue()方法
  应返回假,直到它被称为
  为您的图像视图。那么它应该
  设置视图并返回数据
  真正。返回值表示
  是否ViewBinder设置视图
  本身或适配器是否应该
  通过其默认绑定数据本身
  行为。


由于我没有返回true,它的工作不正确。
现在,它完美的作品。

I would like to display an icon in my ListView depending on the database value. I follow this answer to do so. But in result nothing is displayed. Here is what I have in my row.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

    <ImageView
        android:id="@+id/movie_subscribed_icon"
        android:padding="3dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/star_off"/>

    <LinearLayout 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical">

     <TextView android:id="@+id/movie_name"
     ...

and here is the code:

    movies.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
          int viewId = view.getId();
          switch(viewId) {
          case R.id.movie_name:
                  int readValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
                  if (readValue == 1) { // viewed movie item
                      TextView movieName = (TextView) view;
                      movieName.setTextColor(Color.GRAY);
                  }
                  break;
          case R.id.movie_subscribed_icon:
              int subscribedValue = cursor.getInt(cursor.getColumnIndexOrThrow(MoviesDbAdapter.KEY_READ));
                  if (subscribedValue > 0) { // subscribed movie item
                      ImageView movieIcon = (ImageView) view;
                      movieIcon.setImageResource(R.drawable.star_off);
                  }
              break;
          }
          return false;
        }

      } );

I especially use the same icon in my code as default one. What is wrong here? (I have star_off in drawable-hdpi and drawable-mdpi folders only)

Upd. the following code works well:

movieIcon.setImageDrawable(getResources().getDrawable(R.drawable.star_off));

解决方案

I've found a solution in another question.

Basically the setViewValue() method should return false until it is called for your image view. Then it should set the data in the view and return true. The return value indicates whether the ViewBinder set the view itself or whether the adapter should bind the data itself via its default behavior.

Since I didn't return true, it worked incorrectly. Now it works perfectly.

这篇关于为什么setImageResource显示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆