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

android如何使用CursorAdapter填充listView并回收它们,android,sqlite,android-listview,android-sqlite,android-cursoradapter,Android,Sqlite,Android Listview,Android Sqlite,Android Cursoradapter,我曾经用arrayAdapter填充listView,并对它们进行回收,以加快速度并减少ram使用。 我想用CursorAdapter做同样的事情,我的意思是我想用图像读取数据(大量数据,大约500行)并显示它们。 最好的方法是什么?我应该做什么,在哪里可以学习做什么。 我已经搜索过了,但我现在真的很困惑,我是android的新手 谢谢下面是一个带有注释的示例,向您展示了如何在列表中包含两个文本视图的光标或适配器中回收视图 下面是一个类ViewHolder,用于保存元素并调用它们一次 publi

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


谢谢

下面是一个带有注释的示例,向您展示了如何在列表中包含两个文本视图的
光标或适配器中回收视图

下面是一个类
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;
} 
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)));

}
在您的
bindView

@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;
} 
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
class

公共类数据库持有者{

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_键和整个curseradapter代码?我不能使用它DatabaseHolder是我的数据库类,我有我的列,比如TITLE和GENRE…你只需使用ViewHolder,将行的标记分配给holder,然后使用holder来访问将值签名到你的元素我已经做了7个小时了,仍然没有。你也可以发布你的DatabaseHelper吗?我已经添加了database类,希望你觉得它有用