0

我创建了一个工作 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;
  }

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

4

1 回答 1

1

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

于 2013-04-21T02:05:06.083 回答