android– 如何覆盖CursorAdapter bindView

android– 如何覆盖CursorAdapter bindView,第1张

概述我正在尝试从ListView中的Cursor显示信息,每行包含一个ImageView和一个TextView.我有一个CustomCursorAdapter扩展CursorAdapter,在bindView中我评估光标中的数据并根据它设置视图图像和文本.当我运行应用程序时,ListView显示正确的行数,但它们是空的.我知道我在覆盖bindView时遗漏了一些

我正在尝试从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    }  }}
最佳答案bindVIEw()方法似乎没问题.

尝试替换newVIEw()方法:

@OverrIDepublic VIEw newVIEw(Context context,VIEwGroup parent) {    return mInflater.inflate(R.layout.row,parent,false);}

并且,出于性能原因:

>在中移动getLayoutInflater()
构造函数
>与所有cursor.getColumnIndexOrThrow()调用相同,
已经说过了
评论
>使用StringBuilder创建lbl文本
>没有必要做Integer.toString(warningThreshold)……
只需使用warningThreshold

稍后编辑:
你的inflate()方法与我建议的方法之间的唯一区别是,这个方法创建了与父级匹配的布局参数. 总结

以上是内存溢出为你收集整理的android – 如何覆盖CursorAdapter bindView全部内容,希望文章能够帮你解决android – 如何覆盖CursorAdapter bindView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1139706.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存