【问题标题】:Android viewBinder displaying images on simpleCursorAdapterAndroid viewBinder 在 simpleCursorAdapter 上显示图像
【发布时间】:2013-04-14 04:22:27
【问题描述】:

我创建了一个工作 ViewBinder 与我的 simpleCursorAdapter 一起使用,并且一切正常。所需的图像按应有的方式显示等。但是当我测试我的代码时,我在我的 viewbinder 中放置了一个日志,其中显示了显示图像的标准。当我查看 logCat 时,它显示了两次迭代的结果,如下所示。 (只有五个条目)。这反过来会创建我的 if 语句和结果图像显示的两次迭代。这不是显示问题,但它会产生一些冗余,因为它会在 listView 中显示图像两次。

日志文件:

columnIndex=1  categoryIdentifier = Supplier
columnIndex=1  categoryIdentifier = Customer
columnIndex=1  categoryIdentifier = Other
columnIndex=1  categoryIdentifier = Other
columnIndex=1  categoryIdentifier = Other
columnIndex=1  categoryIdentifier = Supplier
columnIndex=1  categoryIdentifier = Customer
columnIndex=1  categoryIdentifier = Other
columnIndex=1  categoryIdentifier = Other
columnIndex=1  categoryIdentifier = Other

运行viewBinder的代码是这样的:

代码:

private void fillData() {

    //The desired columns to be bound: 
    String[] from = new String[] { ContactsDB.COLUMN_CATEGORY, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
    //The XML views that the data will be bound to:
      int[] to = new int[] {R.id.contact_icon, R.id.label2, R.id.label};

   getLoaderManager().initLoader(0, null, this);
   adapter = new SimpleCursorAdapter(this, R.layout.contact_row, null, from, to, 0);
   // Set the ViewBinder to alternate the image in the listView
   adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
       public boolean setViewValue(View view, Cursor cursor, int columnIndex) {         
          // int viewId = view.getId();
           int categoryIndex = cursor.getColumnIndexOrThrow(ContactsDB.COLUMN_CATEGORY);

           if(columnIndex == categoryIndex)        
           {  
               String categoryIdentifier = cursor.getString(columnIndex);
               //switch categoryIdentifier
              if(categoryIdentifier.equalsIgnoreCase("Supplier")){
                  displayImage = (ImageView) view;
                  displayImage.setImageResource(R.drawable.supplier);
              }
              if(categoryIdentifier.equalsIgnoreCase("Other")){
                  displayImage = (ImageView) view;
                  displayImage.setImageResource(R.drawable.other);
              }    
               Log.v("TEST COMPARISON", "columnIndex=" + columnIndex + "  categoryIdentifier = " + categoryIdentifier);  

             return true;          
         } 
         return false;      
         } 
       });

    setListAdapter(adapter);
  } // end of fillData

  // Sort the names by last name, then by first name
  String orderBy = ContactsDB.COLUMN_LAST_NAME + " COLLATE NOCASE ASC"
  + "," + ContactsDB.COLUMN_FIRST_NAME + " COLLATE NOCASE ASC" ;

  // Creates a new loader after the initLoader () call
  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    //String[] projection = { ContactsDB.ROW_ID, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
      String[] projection = { ContactsDB.ROW_ID, ContactsDB.COLUMN_CATEGORY, ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };
    CursorLoader cursorLoader = new CursorLoader(this,
    whateverContentProvider.CONTENT_URI, projection, null, null, orderBy);
    return cursorLoader;
  }

有人知道为什么会有这种冗余吗?

【问题讨论】:

    标签: java android listview android-viewbinder


    【解决方案1】:

    将您的列表视图 android:layout_height 更改为 match_parent

    【讨论】:

    • 谢谢,成功了!我没有考虑 XML,但它是有道理的。
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2012-10-31
    • 2014-08-26
    • 2012-04-14
    • 1970-01-01
    • 2015-08-24
    相关资源
    最近更新 更多