【问题标题】:android-How to use CursorAdapter to fill listView and recycling themandroid-如何使用CursorAdapter填充listView并回收它们
【发布时间】:2014-06-20 14:29:10
【问题描述】:

我曾经用 arrayAdapter 填充 listView 并回收它们以加快速度并减少 ram 使用。 我想对 CursorAdapter 做同样的事情,我的意思是我想用图像读取数据(大量数据,大约 500 行)并显示它们。 做这个的最好方式是什么 ?我应该做什么以及在哪里可以学习做什么。 我已经搜索过,但我现在真的很困惑,我是 android 的新手。

谢谢

【问题讨论】:

    标签: android sqlite android-listview android-sqlite android-cursoradapter


    【解决方案1】:

    这是一个带有 cmets 的示例,向您展示如何回收 CursorAdapter 中的视图,该视图具有 2 个文本视图作为列表中的项目。

    这里是 ViewHolder 类,用于保存元素并调用它们一次。

    public class ViewHolder {
    
        TextView tvTitle, tvGenre;
        public ViewHolder(View row)
        {
            tvTitle = (TextView) row.findViewById(R.id.tvTitle);
            tvGenre = (TextView) row.findViewById(R.id.tvGenre);
        }
    
    
    }
    

    在你的newView

    @Override
    public View newView(Context context, Cursor arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
    
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_row, arg2, false);
    
        //create an instance of the holder
        ViewHolder holder = new ViewHolder(row);
        //add it to the Tag, so if it not null get the elements from tag.   
        row.setTag(holder);
    
        return row;
    } 
    

    在你的bindView

    Override
    public void bindView(View v, Context context, Cursor c) {
        // TODO Auto-generated method stub
    
        //create an instance of the class and assign its value from view tag
        ViewHolder holder = (ViewHolder) v.getTag();
    
        //use the holder to assign values to the elements   
        holder.tvTitle.setText(c.getString(c.getColumnIndex(DatabaseHolder.TITLE_KEY)));
        holder.tvGenre.setText(c.getString(c.getColumnIndex(DatabaseHolder.GENRE_KEY)));
    
    }
    

    希望这个例子对你来说清楚,根据你的Item改变它。

    ...

    更新:基于要求苛刻的DatabaseHolder

    公共类 DatabaseHolder {

    private final static String DATABASE_NAME = "MoviesDatabase";
    private final static String TABLE_NAME = "MoviesTable";
    private final static int VERSION = 1;
    
    public final static String ID_KEY = "_id";
    public final static String TITLE_KEY = "title";
    public final static String GENRE_KEY = "genre";
    public final static String DESC_KEY = "description";
    
    public final static String[] COLUMNS = {ID_KEY, TITLE_KEY, GENRE_KEY, DESC_KEY};
    
    private Context  context;
    private SQLiteDatabase myDB;
    private OpenHelper helper;
    
    public DatabaseHolder(Context context)
    {
        this.context = context;
    }
    
    public class OpenHelper extends SQLiteOpenHelper {
    
        Context context;
    
        public OpenHelper(Context context) {
            super(context, TABLE_NAME, null, VERSION);
            // TODO Auto-generated constructor stub
            this.context = context;
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
    
            try {
                db.execSQL("CREATE TABLE "+ TABLE_NAME +" ( " +
                        ID_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        TITLE_KEY + " TEXT(32) NOT NULL, " +
                        GENRE_KEY + " TEXT(32) NOT NULL, " +
                        DESC_KEY + " TEXT(512) NOT NULL);"
                        );
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                Message.message(context, "Failed"+e);
            }
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    
    
    }
    
    public DatabaseHolder open()
    {
        helper = new OpenHelper(context);
        myDB = helper.getWritableDatabase();
        return this;
    }
    
    public long insertMovie(String title, String genre, String desc) {
    
        ContentValues cv = new ContentValues();
    
        cv.put(TITLE_KEY, title);
        cv.put(GENRE_KEY, genre);
        cv.put(DESC_KEY, desc);
    
        return myDB.insert(TABLE_NAME, null, cv);
    
    }
    
    public Cursor getAllRowCursor() {
    
        Cursor c = myDB.query(TABLE_NAME, COLUMNS, null, null, null, null, null);
    
        if (c != null)
            c.moveToFirst();
    
        return c;
    }
    
    public Cursor getSpecificRows(int id) {
    
        String where = ID_KEY + " = " + id;
        Cursor c = myDB.query(TABLE_NAME, COLUMNS, where, null, null, null, null);
    
        if (c != null)
            c.moveToFirst();
    
        return c;
    
    }
    
    public Cursor getFilteredRows(String result)
    {
        result = "'" + result.trim() + "%'";
        String where = TITLE_KEY + " LIKE " + result;
        Cursor c = myDB.query(TABLE_NAME, COLUMNS, where, null, null, null, null);
    
        if (c != null)
            c.moveToFirst();
    
        return c;
    
    
    }
    
    public void truncate() {
        // TODO Auto-generated method stub
    
        myDB.delete(TABLE_NAME, null, null);
    }
    }
    

    【讨论】:

    • 谢谢你,你能给我举个例子,这部分代码、DatabaseHolder.TITLE_KEY 和整个curseradapter 代码的背后是什么?我无法使用它
    • DatabaseHolder 是我的数据库类,其中我有 TITLE 和 GENRE 等列...您只需使用 ViewHolder 并将行的标签分配给持有者,然后使用持有者分配值到你的元素
    • 我已经工作了 7 个小时,仍然没有。能否请您发布您的 DatabaseHelper 吗?
    • 我已经添加了数据库类,希望对你有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2016-01-08
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多