gpt4 book ai didi

android - 如何使用 CursorAdapter?

转载 作者:行者123 更新时间:2023-11-29 22:11:13 24 4
gpt4 key购买 nike

我不确定问题标题是否合适,但是在搜索了很多之后我问了这个。

在我的 SQLite 表中,我有列

1: _id   2: position   3: path

position: the position of the gridView where the Image is to set
path: the path of the SDCard having corresponding image

我如何从路径中获取图像并设置到 GridView 中

GridView grid = (GridView) findViewById(R.id.play_grid_view);
DBAdapter adapter = new DBAdapter(this); //My costum adapter for databse operation
adapter.open();
Cursor cusor = adapter.getAllImages(); //returns cursor with 3 columns mentioned above
startManagingCursor(cusor);

这之后我应该做什么?

最佳答案

如果您的 position 列表示图像在网格中的位置,您可以只使用此列对查询进行排序,然后 cursorAdapter 将根据您在数据库中设置的位置填充您的网格。

这是不可用的,如果您将跳过一些 gridview 单元格(假设您的数据库中有以下位置:1,2,4 - 那么您的此适配器将填充位置 1,2,3,因为实际上没有位置检查)

public class ImageCursorAdapter extends CursorAdapter {
public ImageCursorAdapter(Context context, Cursor c) {
super(context, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
String pos = cursor.getString(positionColumnIndex);
String path = cursor.getString(pathColumnIndex);
ImageView image = (ImageView)view;
image.setImageDrawable(Drawable.createFromPath(path));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = new ImageView(context);
bindView(v, context, cursor);
return v;
}
}

关于android - 如何使用 CursorAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9582965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com