如何重写 CursorAdapterbindView

发布于 2024-10-04 12:19:15 字数 1661 浏览 1 评论 0原文

我正在尝试在 ListView 中显示来自 Cursor 的信息,每行包含一个 ImageView 和一个 TextView 。我有一个 CustomCursorAdapter 扩展 CursorAdapter,在 bindView 中,我评估来自光标的数据,并基于该数据设置视图图像和文本。

当我运行应用程序时,ListView 显示正确的行数,但它们是空的。我知道我在重写 bindView 时错过了一些东西,但我不确定是什么。

任何帮助将不胜感激。

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
    }
  }
}

I'm trying to display information from a Cursor in a ListView, each row contains a ImageView and a 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.

When I run the app the ListView displayes the correct amount of rows, but they are empty. I know I have missed something when overriding bindView, but I'm not sure what.

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
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2024-10-11 12:19:15

bindView() 方法看起来没问题。

尝试替换您的 newView() 方法:

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

并且,出于性能原因:

  • getLayoutInflater() 移至
    构造函数
  • 与所有 cursor.getColumnIndexOrThrow() 调用相同,
    正如 已完成 中已经说过的
    注释
  • 使用 StringBuilder 来创建您的 lbl 文本,
  • 无需执行 Integer.toString(warningThreshold)...
    只需使用warningThreshold

稍后编辑
您的 inflate() 方法与我建议的方法之间的唯一区别是,该方法创建与父级匹配的布局参数。

The bindView() method seems ok.

Try replacing your newView() method:

@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
  • same thing with all the cursor.getColumnIndexOrThrow() calls,
    as ened already said in the
    comments
  • use a StringBuilder for creating your lbl text
  • there's no need to do Integer.toString(warningThreshold)...
    just use 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文