【问题标题】:Android - Wanting to alternate icon images in a listview using cursorLoaderAndroid - 想要使用 cursorLoader 在列表视图中替换图标图像
【发布时间】:2013-04-20 01:50:26
【问题描述】:

我有一个应用程序,它显示按姓氏排序的联系人列表视图,然后是名字。每个联系人旁边都有一个图像(图标)。有 3 种联系人我想显示 3 个不同的图像(客户/供应商/其他)我现在有一个默认图像,设置为客户。我想知道是否有一种方法可以使用下面显示的 cusorLoader 来动态交替图像,或者是否最好在我的 onResume 中添加一个涉及光标的方法。 (每次我需要显示图像时都会调用 onResume)。我相信 simpleCursorAdapter 只能将 textViews 作为参数,所以如果可能的话,也许复合 textview/image 会起作用。我的图标不存储在数据库中,只是在可绘制对象中。

提前感谢您的任何回复。

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

} 

【问题讨论】:

    标签: java android image cursor


    【解决方案1】:

    这是我用来在 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;
            }
        });
    

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

    【讨论】:

    • 感谢 Yoann 的想法,但我最终创建了一个绑定到 simpleCursorAdapter 的自定义适配器。我给你的答复是接受的答案,因为它看起来很有用,但我读到这种类型的实现可能不适用于滚动列表视图。 (我有)
    • 其实我是用这个来做可滚动的ListViews的,为什么这个方法可能效果不好?有已知问题吗?谢谢!
    • Yoann - 我刚回到它(周末)并意识到我不需要自定义适配器,因为我使用的是 simpleCursorAdapter。如您所示,我最终使用了 viewBinder 的变体。对于任何感兴趣的人来说,工作示例都在这里:[link]stackoverflow.com/questions/16127243/…
    猜你喜欢
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多