CursorAdapter bindView 优化 [英] CursorAdapter bindView optimization

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

问题描述

当覆盖 ArrayAdapter 时,我知道使用这样的模式是正确的:

When overriding ArrayAdapter I know is correct using a pattern like this:

if(view != null){
   ...create new view setting fields from data 
}else
  return view; //reuse view

将此模式与 CursorAdapters 一起使用是否正确?我的问题是,根据光标字段,我有一个文本颜色,它可以是红色或蓝色,所以我不希望在具有需要蓝色字段的单元格上出现任何错误,例如红色.我的 bindView 代码是这样的:

is correct too using this pattern with CursorAdapters? My problem is that I have a textcolor which can be red or blue according to a cursor field, so I don't want any errors like a red color on a cell which has a field needing blue color. My bindView code is something like this:

if(c.getString(2).equals("red"))
      textView.setTextColor(<red here>);
   else
      textView.setTextColor(<blue here>);

如果我重用视图,我可以确定红色变为红色,而蓝色变为蓝色吗?

if I reuse view can I be sure that red goes on red, while blue goes on blue?

推荐答案

CursorAdapter中,你在newView中获得布局,在bindView中绑定数据>.CursorAdapter 已经在 getView 中做了重用模式,所以你不必再做.下面是原始的 getView 源代码.

In CursorAdapter, you get layout in newView and bind data in bindView. CursorAdapter already do reuse pattern in getView so you don't have to do it again. Below is the original getView source code.

  public View getView(int position, View convertView, ViewGroup parent) {
    if (!mDataValid) {
        throw new IllegalStateException("this should only be called when the cursor is valid");
    }
    if (!mCursor.moveToPosition(position)) {
        throw new IllegalStateException("couldn't move cursor to position " + position);
    }
    View v;
    if (convertView == null) {
        v = newView(mContext, mCursor, parent);
    } else {
        v = convertView;
    }
    bindView(v, mContext, mCursor);
    return v;
}

如果您想使用 ViewHolder Pattern 进一步优化这里是示例:在 newView 中创建标签并在 bindView

If you want further optimization using ViewHolder Pattern here is example: Create tag in newView and retrieve in bindView

    public class TimeListAdapter extends CursorAdapter {
     private LayoutInflater inflater;
     private    static  class   ViewHolder  {
         int    nameIndex;
         int    timeIndex;
         TextView   name;
         TextView   time;
    }
  public TimeListAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
  this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
         ViewHolder holder  =   (ViewHolder)    view.getTag();
         holder.name.setText(cursor.getString(holder.nameIndex));
         holder.time.setText(cursor.getString(holder.timeIndex));
  }
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup  
  p parent) {
         View   view    =   inflater.inflate(R.layout.time_row, null);
         ViewHolder holder  =   new ViewHolder();
         holder.name    =   (TextView)  view.findViewById(R.id.task_name);
         holder.time    =   (TextView)  view.findViewById(R.id.task_time);
     holder.nameIndex   =   cursor.getColumnIndexOrThrow 
         (TaskProvider.Task.NAME);
         holder.timeIndex   =   cursor.getColumnIndexOrThrow    
         (TaskProvider.Task.DATE);
         view.setTag(holder);
    return view;
  }
}

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

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