次のようなgetViewを備えたカスタムSimpleCursorAdapterがあります。
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();
}
}
}
}
奇妙に見える場合は、名前を非表示にしているだけです。