Android viewBinder在simpleCursorAdapter上显示图像

时间:2013-04-21 02:02:07

标签: java android listview android-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的代码是:

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

任何人都知道为什么会出现这种冗余?

1 个答案:

答案 0 :(得分:1)

将您的listview android:layout_height更改为match_parent