android-如何使用CursorAdapter填充listView并回收

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

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

谢谢

最佳答案

这是一个带有注释的示例,向您展示如何回收 CursorAdapter 中的 View ,其中有 2 个 TextView 作为列表中的一个项目。

这里是 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)));

}

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

...

更新:基于要求较高的DatabaseHolder

公共(public)类 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);
}
}

关于android-如何使用CursorAdapter填充listView并回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23452969/

相关文章:

android - 仅重绘 Android View 中的一部分

android - Android 小部件类型示例

android - 将 .aar 文件集成到多模块 android studio 项目中

Java 准备好的语句未输入数据并命中捕获

android - 按 ListViewItem 时谷歌地图的错误协调

android - 如何在 Android 中定位 ListView?

MvvmCross DataBinding 修改 Android ListView 中的单个项目

android - 使用Robolectric而不进行多义处理?

ios - SQLite初始化的位置? (iOS)

ios - 滚动到 View 中时,菜单的屏幕外文本会出现延迟 - WebView 问题? (iOS)