Android Listview 与 sqlite 和 CursorAdapter

标签 android database performance sqlite listview

经过大约 12 个小时的工作,我成功地使用著名的 Google video 的超快速代码编写了一个工作游标适配器类。 ,此类从数据库填充 ListView ,每行一个文本和一个图片。 但我有一种感觉,我在类(class)结束时添加的代码破坏了谷歌在性能方面的努力,以填充文本和图片,并且当我使用真实/最终数据库时,我无法判断性能。 ..(c也是 Activity 类中制作的光标) 以下是 2 个子类:

    public class CursorLi extends CursorAdapter {
    LayoutInflater minflater;
    int imd;

        public CursorLi(Context context, Cursor c, boolean autoRequery) {
            super(context, c, autoRequery);
            minflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);     
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            if (convertView == null){           
                convertView = minflater.inflate(R.layout.lvitem, parent, false);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.litext);
                holder.icon = (ImageView) convertView.findViewById(R.id.fiig);
                convertView.setTag(holder);
            } else{
                holder = (ViewHolder) convertView.getTag();
            }
            c.moveToPosition(position);
            imd = getResources().getIdentifier(c.getString(6), "drawable", "com.example.myapp");
            dr = getResources().getDrawable(imd);
            holder.text.setText(c.getString(4));
            holder.icon.setImageDrawable(dr);
            return convertView;
        }
        @Override
        public void bindView(View view, Context context, Cursor cursor) {}
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) { return null;    }
    }

public static class ViewHolder {
    TextView text;
    ImageView icon;
    }

你们能帮我优化一下代码吗? 特别是:

  1. 为什么我必须添加“c.moveToPosition(position);” ?我以为 光标适配器管理位置...

  2. 是否有更快的方法将图片应用到行的 imageview ?我用了3行代码 (数据库包含图片文件名)。

  3. getString 是最好的方法吗?

  4. 单独的类还是子类更好?当类(class)是一个 单独的类我不知道如何获取光标数据 getView() 函数,所以现在它是 Activity 的子类, 光标已全局声明。

如果你想问我为什么不使用 simplecursoradapter,因为我想使用 google 代码,以提高性能,因为列表可能有 300 行...... 谢谢

最佳答案

  1. 对于 CursorAdapter,您只能重写 newViewbindView,而不是 getViewCursorAdaptergetView 的默认实现会为您执行 cursor.moveToPosition 操作。
  2. 我不明白你的意思。
  3. getString 没问题。
  4. 阅读1。为了管理游标,我建议使用CursorLoader。 (参见:https://developer.android.com/training/load-data-background/setup-loader.html)

您的适配器应如下所示。

public class CursorLi extends CursorAdapter {

    private LayoutInflater minflater;
    private int imd;

    public CursorLi(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        minflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = minflater.inflate(R.layout.lvitem, parent, false);
        ViewHolder holder = new ViewHolder();
        holder.text = (TextView) view.findViewById(R.id.litext);
        holder.icon = (ImageView) view.findViewById(R.id.fiig);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        imd = getResources().getIdentifier(c.getString(6), "drawable", "com.example.myapp");
        dr = getResources().getDrawable(imd);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(c.getString(4));
        holder.icon.setImageDrawable(dr);
    }

    public static class ViewHolder {
        TextView text;
        ImageView icon;
    }

}

关于Android Listview 与 sqlite 和 CursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21521969/

相关文章:

android - CollapsingToolbarLayout 内的工具栏,工具栏标题不显示

android - 如何在 Kotlin 的枚举类上泛化函数?

mysql_real_escape_string 错误

sql - 使用 SUM() 或缓存

sql - 提高查询速度 : simple SELECT in big postgres table

android - 配置模拟器 DNS 设置

android - NDK 和 libiconv

database - SQLite3 : Delete record without overwriting it with nullbytes

mysql - 在 Excel 中引用表格的子集(列)

multithreading - Intel Nehalem 单线程峰值性能