如何覆盖的CursorAdapter bindView [英] How to override CursorAdapter bindView

查看:234
本文介绍了如何覆盖的CursorAdapter bindView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从显示在光标信息的ListView ,每行包含一个的ImageView 的TextView 。我有一个 CustomCursorAdapter 延长的CursorAdapter bindView 我评估从游标中的数据,并基于该设置的意见,图片和文字。

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

任何帮助将大大AP preciated。

 私有类CustomCursorAdapter扩展的CursorAdapter {  公共CustomCursorAdapter(){
    超(Lmw.this,monitorsCursor);
  }  @覆盖
  公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    LayoutInflater layoutInflater = getLayoutInflater();    返回layoutInflater.inflate(R.layout.row,NULL);
  }  @覆盖
  公共无效bindView(查看视图,上下文的背景下,光标光标){
    尝试{
      INT monitorNameIndex = cursor.getColumnIndexOrThrow(DbAdapter.MONITORS_COLUMN_MONITOR_NAME);
      INT resultTotalResponseTimeIndex = cursor.getColumnIndexOrThrow(DbAdapter.RESULTS_COLUMN_TOTAL_RESPONSE_TIME);      字符串monitorName = cursor.getString(monitorNameIndex);
      INT warningThreshold = cursor.getInt(resultTotalResponseTimeIndex);      字符串LBL = monitorName +\\ n+ Integer.toString(warningThreshold)+毫秒;      TextView的标签=(TextView中)view.findViewById(R.id.label);
      label.setText(LBL);      ImageView的图标=(ImageView的)view.findViewById(R.id.icon);
      如果(warningThreshold< 1000){
        icon.setImageResource(R.drawable.ok);
      }其他{
        icon.setImageResource(R.drawable.alarm);
      }
    }赶上(抛出:IllegalArgumentException五){
      // TODO:处理异常
    }
  }
}


解决方案

bindView()方法似乎确定。

尝试更换您的 NewView的()的方法:

  @覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    返回mInflater.inflate(R.layout.row,父母,假);
}

和,由于性能原因:


  • getLayoutInflater()
    构造

  • 所有的 cursor.getColumnIndexOrThrow()调用同样的事情,
    ened 已在结果说
    注释

  • 使用StringBuilder创建您的 LBL 的文本

  • 有没有必要做的 Integer.toString(warningThreshold)的...
    只是使用的 warningThreshold

后来修改
唯一的区别你的膨胀()方法,我建议之一是,这一个创建符合母公司的布局PARAMS。

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

解决方案

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.

这篇关于如何覆盖的CursorAdapter bindView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆