0

I have an application that displays a listView of contacts sorted by Last, then first names. Beside each contact is an image (icon). There are 3 kinds of contacts for which I'd like to display 3 different images (customers/suppliers/other) I have a default image now that is set to customer. I'm wondering if there's a way using the cusorLoader shown below to alternate images on the fly, or whether it would just be best to add a method involving a cursor in my onResume. (onResume is called each time I need to display the images). I believe simpleCursorAdapter can only take textViews as args, so if it's possible, maybe a compound textview/image would work. My icons are not stored in the database, just in the drawables.

Thanks in advance for any replies.

  @Override
  protected void onResume() {
   super.onResume();
   //Starts a new or restarts an existing Loader in this manager
   getLoaderManager().restartLoader(0, null, this);
  }

  /*
   * The fillData method binds the simpleCursorAadapter to the listView.
   */

  private void fillData() {

    String[] from = new String[] { ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };

    //The XML views that the data will be bound to:
    int[] to = new int[] {R.id.label2, R.id.label};

    getLoaderManager().initLoader(0, null, this);
    adapter = new SimpleCursorAdapter(this, R.layout.contact_row, null, from,
        to, 0);
    setListAdapter(adapter);
  }
  // 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 };
    CursorLoader cursorLoader = new CursorLoader(this,
    SomeContentProvider.CONTENT_URI, projection, null, null, orderBy);
    return cursorLoader;
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.
    // (The framework will take care of closing the old cursor once we return.)
    adapter.swapCursor(data); //Call requires Min API 11
  }

  @Override
  public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.    
    // Data is no longer available, delete the reference
    adapter.swapCursor(null);
  }

} 
4

1 回答 1

2

这是我用来在 ListView 上动态显示可绘制对象的代码,您必须在适配器上使用函数 setViewBinder:

mAdapter.setViewBinder(new ViewBinder() {
        public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

            //Modification of the icon to display in the list
            if (aColumnIndex == aCursor.getColumnIndex(DatabaseHandler.RATE_EMOTION)) {
                int emotionID = aCursor.getInt(aColumnIndex);
                Drawable emotionDrawable = resources.getDrawable(R.drawable.ic_unknown_rate);

                //if emotion is set
                if(emotionID > 0){
                    String emotionDrawablePath = "ic_smi" + emotionID;          
                    int emotionDrawableID = resources.getIdentifier(emotionDrawablePath,"drawable", getPackageName());
                    //if a drawable is found
                    if(emotionDrawableID > 0){
                        emotionDrawable = resources.getDrawable(emotionDrawableID);
                    }
                }

                ImageView emotionImage = (ImageView) aView;                 
                emotionImage.setImageDrawable(emotionDrawable);
                return true;
            }

            return false;
        }
    });

您可以在此示例中看到,我根据从光标获得的每一行的数据更改了可绘制对象。

于 2013-04-20T03:17:25.760 回答