0

次のような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();
                }
            }
        }
    }

奇妙に見える場合は、名前を非表示にしているだけです。

4

2 に答える 2

0

最初に行うこと:

c.moveToFirst();

任意のデータにアクセスする前に、必要な残りを行います。getView() は、入力を実行するときに複数回呼び出されるため、すべて同じになるため、あなたの場合はc.moveToFirst()、内ではなく外で呼び出します。getView()おそらく、カーソルを返すメソッドで呼び出します。

c.moveToNext();アクセスを行うときは、 getView() 内でこれを行う必要がある次のデータのカーソルを移動することを忘れないでください。

于 2011-07-21T02:11:48.980 に答える
0

CursorAdapter のセットアップ方法で機能する良い答えはありません。

ListActivity から AsyncTask を 1 回だけ実行する必要があります。ダウンロードが保留されているデータベース内のすべての画像を A​​syncTask でダウンロードして、データベースを更新することはできますか? onProgressUpdate を使用して ListActivity のメソッドを呼び出し、notifyDataSetchanged() を実行できます。

必要なのは、 notifyDataSetChanged()と notifyDataSetInvalidated() です。asyncTask onPostExecute メソッドは、新しい/更新されたカーソルを ListActivity に返し、ListActivity から notifyDataSetchanged() および notifyDataSetInvalidated を呼び出す必要があります。

編集: これと同じ質問。

于 2011-07-21T02:30:19.150 に答える