0

我有一个自定义的 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();
                }
            }
        }
    }

好吧,如果它看起来很奇怪,我只是隐藏了一些名字。

4

2 回答 2

0

首先做:

c.moveToFirst();

在访问任何数据之前,然后做你想做的其余部分。在你的情况下 call c.moveToFirst(),不是在getView()里面而是在外面,因为你会得到相同的结果,因为 getView() 在执行填充时被多次调用。也许,在返回光标的方法中调用它。

当您进行访问时,不要忘记移动光标以获取c.moveToNext();您必须在 getView() 中执行此操作的下一个数据;

于 2011-07-21T02:11:48.980 回答
0

我没有一个很好的答案可以与您的 CursorAdapter 的设置方式一起使用。

您应该只从 ListActivity 运行一次 AsyncTask。也许让 AsyncTask 下载数据库中所有待下载的图像并更新数据库?您可以使用 onProgressUpdate 调用 ListActivity 中的方法来执行 notifyDataSetchanged()。

你想要的是notifyDataSetChanged()和 notifyDataSetInvalidated()。您的 asyncTask onPostExecute 方法应将新的/更新的游标返回给 ListActivity,然后从 ListActivity 调用 notifyDataSetchanged() 和 notifyDataSetInvalidated。

编辑: 与此相同的问题。

于 2011-07-21T02:30:19.150 回答