简体   繁体   English

如何覆盖CursorAdapter bindView

[英]How to override CursorAdapter bindView

I'm trying to display information from a Cursor in a ListView , each row contains a ImageView and a TextView . 我正在尝试从ListViewCursor显示信息,每行包含一个ImageView和一个TextView I have a CustomCursorAdapter extending CursorAdapter , in bindView I evaluate the data from the cursor and based on that set the views image and text. 我有一个CustomCursorAdapter扩展CursorAdapter ,在bindView我评估光标中的数据并根据它设置视图图像和文本。

When I run the app the ListView displayes the correct amount of rows, but they are empty. 当我运行应用程序时, ListView显示正确的行数,但它们是空的。 I know I have missed something when overriding bindView, but I'm not sure what. 我知道我在覆盖bindView时遗漏了一些东西,但我不确定是什么。

Any help would be greatly appreciated. 任何帮助将不胜感激。

private class CustomCursorAdapter extends CursorAdapter {

  public CustomCursorAdapter() {
    super(Lmw.this, monitorsCursor);
  }

  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater layoutInflater = getLayoutInflater();

    return layoutInflater.inflate(R.layout.row, null);
  }

  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    try {
      int monitorNameIndex = cursor.getColumnIndexOrThrow(DbAdapter.MONITORS_COLUMN_MONITOR_NAME);
      int resultTotalResponseTimeIndex = cursor.getColumnIndexOrThrow(DbAdapter.RESULTS_COLUMN_TOTAL_RESPONSE_TIME);

      String monitorName = cursor.getString(monitorNameIndex);
      int warningThreshold = cursor.getInt(resultTotalResponseTimeIndex);

      String lbl = monitorName + "\n" + Integer.toString(warningThreshold) + " ms";

      TextView label = (TextView) view.findViewById(R.id.label);     
      label.setText(lbl);

      ImageView icon = (ImageView)view.findViewById(R.id.icon);
      if(warningThreshold < 1000) {
        icon.setImageResource(R.drawable.ok);      
      } else {
        icon.setImageResource(R.drawable.alarm);
      }


    } catch (IllegalArgumentException e) {
      // TODO: handle exception
    }
  }
}

The bindView() method seems ok. bindView()方法似乎没问题。

Try replacing your newView() method: 尝试替换newView()方法:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mInflater.inflate(R.layout.row, parent, false);
}

And, for performance reasons: 并且,出于性能原因:

  • move getLayoutInflater() in the constructor 在构造函数中移动getLayoutInflater()
  • same thing with all the cursor.getColumnIndexOrThrow() calls, as ened already said in the 与所有同样的事情cursor.getColumnIndexOrThrow()调用,如已经说过ened
    comments 评论
  • use a StringBuilder for creating your lbl text 使用StringBuilder创建lbl文本
  • there's no need to do Integer.toString(warningThreshold) ... just use warningThreshold 没有必要做Integer.toString(warningThreshold) ...只需使用warningThreshold

Later edit : The only difference between your inflate() method and the one I suggested is that this one creates layout params that match the parent. 稍后编辑 :你的inflate()方法与我建议的方法之间的唯一区别是,这个方法创建了与父级匹配的布局参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM