【问题标题】:SimpleCursorAdapter with ImageView and TextView带有 ImageView 和 TextView 的 SimpleCursorAdapter
【发布时间】:2011-12-14 19:16:11
【问题描述】:

您能否在带有列表视图的SimpleCursorAdapter 中的一行中使用imageviewtextview 的布局?

这将是布局

<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/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/bowler_txt"
    android:paddingLeft="25dp"
    android:textSize="30dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bowler"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

可以在 SimpleCursorAdapter 中使用列表视图完成吗?当我需要列表视图中的图像时,我总是使用自定义数组适配器,但从不使用光标。

如果可以的话,我将如何设置图像?

【问题讨论】:

    标签: android listview simplecursoradapter


    【解决方案1】:

    当要绑定的视图是ImageView 并且没有现有的ViewBinder 关联SimpleCursorAdapter.bindView() 时,调用setViewImage(ImageView, String)。 默认情况下,该值将被视为图像资源。如果该值不能用作图像资源,则将该值用作 图像 Uri

    如果您需要以其他方式过滤从数据库中检索到的值,您需要将ViewBinder 添加到ListAdapter,如下所示:

    listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
       /** Binds the Cursor column defined by the specified index to the specified view */
       public boolean setViewValue(View view, Cursor cursor, int columnIndex){
           if(view.getId() == R.id.your_image_view_id){
               //...
               ((ImageView)view).setImageDrawable(...);
               return true; //true because the data was bound to the view
           }
           return false;
       }
    });
    

    【讨论】:

      【解决方案2】:

      为了扩展@Francesco Vadicamo 的答案,这是一个更大活动的一部分。我把它分开是因为我需要从代码的多个区域调用它。 databaseHandlerlistView 被定义为类变量,并在 onCreat() 中初始化。

      private void updateListView() {
          // Get a Cursor with the current contents of the database.
          final Cursor cursor = databaseHandler.getCursor();
      
          // The last argument is 0 because no special behavior is required.
          SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                  R.layout.listview,
                  cursor,
                  new String[] { databaseHandler.ICON, databaseHandler.BOWLER_TXT },
                  new int[] { R.id.icon, R.id.bowler_txt },
                  0);
      
          // Override the handling of R.id.icon to load an image instead of a string.
          adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
              public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                  if (view.getId() == R.id.imageview) {
                      // Get the byte array from the database.
                      byte[] iconByteArray = cursor.getBlob(columnIndex);
      
                      // Convert the byte array to a Bitmap beginning at the first byte and ending at the last.
                      Bitmap iconBitmap = BitmapFactory.decodeByteArray(iconByteArray, 0, iconByteArray.length);
      
                      // Set the bitmap.
                      ImageView iconImageView = (ImageView) view;
                      iconImageView.setImageBitmap(iconBitmap);
                      return true;
                  } else {  // Process the rest of the adapter with default settings.
                      return false;
                  }
              }
          });
      
          // Update the ListView.
          listView.setAdapter(adapter);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多