2013-04-21 65 views
0

我創建了一個可用於我的simpleCursorAdapter的ViewBinder,並且所有功能都正常。所需的圖像顯示爲他們應該等等。但是當我測試我的代碼時,我將我的日誌放入我的視窗中,顯示了顯示圖像的條件。當我看着logCat時,它顯示了兩次迭代的結果,如下所示。 (只有五個條目)。這反過來會創建我的if語句和生成的圖像顯示的兩個迭代。這不是顯示器的問題,但它會產生一些冗餘,因爲它會在listView中兩次顯示圖像。Android viewBinder在simpleCursorAdapter上顯示圖像

日誌文件:

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

將您的列表視圖android:layout_height更改爲match_parent

+0

謝謝,這樣做!我沒有考慮XML,但它是有道理的。 – 2013-04-21 02:09:37