简体   繁体   English

Android viewBinder在simpleCursorAdapter上显示图像

[英]Android viewBinder displaying images on simpleCursorAdapter

I have created a working ViewBinder to use with my simpleCursorAdapter, and all is functioning properly. 我创建了一个可工作的ViewBinder以与我的simpleCursorAdapter一起使用,并且所有功能均正常运行。 The desired images display as they should,etc. 所需的图像按原样显示,等等。 But when I was testing my code, I put a log my in my viewbinder that displays the criteria for displaying the image. 但是,当我测试代码时,我在viewbinder中放置了一条日志,其中显示了显示图像的条件。 When I look at logCat, it shows two iterations of the results as shown below. 当我查看logCat时,它显示了结果的两次迭代,如下所示。 (there are only five entries). (只有五个条目)。 Which would in turn create two iterations of my if statements and resulting image display. 依次将创建两次if语句的迭代,并显示图像。 This isn't a problem with the display, but it would create some redundancy in that it would display the images in the listView twice. 这不是显示的问题,但是会产生一些冗余,因为它将在listView中显示两次图像。

LOG FILE: 日志文件:

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

The code to run the viewBinder is this: 运行viewBinder的代码是这样的:

CODE: 码:

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

Anyone know why there would be this redundancy? 有人知道为什么会有这种冗余吗?

将您的listview android:layout_height更改为match_parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM