我有一个自定义的 SimpleCursorAdapter 与 getView 是这样的:
public View getView(int position, View convertView, ViewGroup parent) {
//this.changeCursor(c); tried this didn't work.
this.c.moveToPosition(position);
int urlCol = c.getColumnIndex("url");
final String url = c.getString(urlCol);
//other code stuff.
imageDownloader.download(url, thumbnail, rowId, db,pb);
imageDownloader 调用 DBAdapter 的更新,但 c.getString(urlCol) 仍然给我以前的值。我尝试将 changeCursor 放在上面的位置,但我仍然得到相同的值。我到底应该在哪里称呼这个?谢谢!
我实际上不希望 ListVIew 重绘我只想将最新数据放入光标中。我将我的 imageview 和 url 传递到 ImageDownloader 类中以下载图像,将位图设置为 imageview 并更新数据库。我注意到光标没有更新,因为 getView 方法返回了相同的数据。
这是我的自定义 simplecursoradapter 声明。上面提到了getview。
public class MessagesCursorAdapter extends SimpleCursorAdapter {
private final ImageDownloader imageDownloader = new ImageDownloader();
ImageDownloader 类
public void download(String url, ImageView imageView, int rowId,
TestDbAdapter db,ProgressBar pb) {
//url can be a http:// or /mnt/sdcard/mnt/ etc etc
//some lenghty code to check if it exists physically on the phone. else....
forceDownload(url, imageView, rowId, db,pb);
private void forceDownload(String url, ImageView imageView, int rowId,
TestDbAdapter db,ProgressBar pb) {
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, rowId,
db,pb);
ImageDownloader 类中的 AsyncTask 类
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private int rowId;
private TestDbAdapter db;
private ProgressBar pb;
private final WeakReference<ImageView> imageViewReference;
public BitmapDownloaderTask(ImageView imageView, int rowId,
TestDbAdapter db,ProgressBar pb) {
imageViewReference = new WeakReference<ImageView>(imageView);
this.rowId = rowId;
this.db = db;
this.pb = pb;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pb.setVisibility(View.VISIBLE);
}
@Override
protected Bitmap doInBackground(String... params) {
url = params[0];
return downloadBitmap(url);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if ((this == bitmapDownloaderTask)) {
int pos = url.lastIndexOf("/");
String fileName = url.substring(pos, url.length());
System.out.println(fileName);
String extLoc = Environment.getExternalStorageDirectory()
+ "/Test";
File folder = new File(extLoc);
if (!folder.exists()) {
folder.mkdir();
}
try {
FileOutputStream out = new FileOutputStream(extLoc
+ fileName);
boolean result = db.updateMessageUrl(rowId, extLoc + fileName);
//should update cursor here, purpose is to change the url link to a physical location on the phone.
bitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[24 * 1024];
options.inJustDecodeBounds = false;
options.inSampleSize = 2;
Bitmap thumbnail = BitmapFactory.decodeFile(extLoc
+ fileName, options);
imageView.setImageBitmap(bitmap);
addBitmapToCache(url, bitmap);
pb.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
好吧,如果它看起来很奇怪,我只是隐藏了一些名字。