Android viewBinder displaying images on simpleCursorAdapter

拥有回忆 提交于 2019-12-25 00:33:31

问题


I have created a working ViewBinder to use with my simpleCursorAdapter, and all is functioning properly. 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. When I look at logCat, it shows two iterations of the results as shown below. (there are only five entries). Which would in turn create two iterations of my if statements and resulting image display. 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.

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:

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?


回答1:


change your listview android:layout_height to match_parent



来源:https://stackoverflow.com/questions/16127243/android-viewbinder-displaying-images-on-simplecursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!