ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 为什么setImageResource什么都不显示?

2019-06-10 10:14:49  阅读:1142  来源: 互联网

标签:android android-listview resources android-imageview


我想在ListView中显示一个图标,具体取决于数据库值.我按照this的答案这样做.
但结果没有显示任何内容.这是我在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"
     ...

这是代码:

    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;
        }

      } );

我特别在我的代码中使用相同的图标作为默认图标.
这有什么不对? (我在drawable-hdpi和drawable-mdpi文件夹中只有star_off)

UPD.以下代码运行良好:

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

解决方法:

public void setImageResource (int resId):这在UI线程上进行Bitmap读取和解码,这可能导致延迟打嗝.如果这是一个问题,请考虑使用setImageDrawable(Drawable)或setImageBitmap(Bitmap)和BitmapFactory.

尝试使图像视图无效,或使用文档建议的替代方法之一.

标签:android,android-listview,resources,android-imageview
来源: https://codeday.me/bug/20190610/1211290.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有