当我启动我的应用程序时,我启动了一个 Asynctask,它会查找任何新视频并创建缩略图并将它们添加到数据库中。后来我使用通用图像加载器来查找该位图并将其设置为视图。我的问题是当我第一次启动我的应用程序时,所有数据都加载到数据库中,但 gridview 中没有显示任何内容。我什至不认为适配器被调用。但是,当我在加载所有数据后启动应用程序时,gridview 会显示所有缩略图并且运行顺畅。
我想知道为什么会发生这种情况以及我能做些什么来解决这个问题。我的代码如下
异步任务
private class DataEntry extends AsyncTask<Void, Integer, GridviewData>{
GridviewData dataentry;
DataEntry(GridviewData gridviewdata){
this.dataentry = gridviewdata;
}
@Override
protected GridviewData doInBackground(Void... params) {
cursor.moveToFirst();
do {
String videoid = cursor.getString(columnindexid);
String videopath = cursor.getString(columnindexdata);
int result = dataentry.findVideoID(videoid);
if (result == 1){
Bitmap bmthumb = ThumbnailUtils.createVideoThumbnail(videopath, MediaStore.Video.Thumbnails.MINI_KIND);
if (bmthumb != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmthumb.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] thumbnailBitmapBytes = stream.toByteArray();
dataentry.addVideoinfo(videoid, videopath, thumbnailBitmapBytes);
}
}
if (result == 0){
Log.i(TAG, "Cursor wasn't processed, no getcount");
}
if(result == 2){
//there is data already there
}
} while (cursor.moveToNext());
Log.i(TAG, "After dowhile loop");
cursor.close();
return dataentry;
}
}
基础图像加载器
public class SqliteImageDownloader extends BaseImageDownloader {
private static final String SCHEME_DB = "db";
private static final String DB_URI_PREFIX = SCHEME_DB + "://";
Cursor cursor;
public SqliteImageDownloader(Context context) {
super(context);
}
@Override
protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
if (imageUri.startsWith(DB_URI_PREFIX)) {
String path = imageUri.substring(19);
cursor = entry.BitFinder(path);
byte[] imageData = cursor.getBlob(0);
return new ByteArrayInputStream(imageData);
} else {
return super.getStreamFromOtherSource(imageUri, extra);
}
}
}
光标适配器
class VideoAdapter extends CursorAdapter {
Cursor curs;
Context context;
public VideoAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
this.curs = c;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
String filepath = curs.getString(videopathindex);
ImageLoader.getInstance().displayImage("db://VideoandThumbs" + filepath, holder.imageview);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View eachgrid = inflater.inflate(R.layout.eachgrid, parent, false);
ViewHolder holder = new ViewHolder();
holder.imageview = (ImageView) eachgrid.findViewById(R.id.Imageview1);
eachgrid.setTag(holder);
return eachgrid;
}
}