Android在哪里调用changecursor?

时间:2011-07-21 02:05:42

标签: android simplecursoradapter

我有一个带有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();
                }
            }
        }
    }

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

2 个答案:

答案 0 :(得分:0)

首先:

c.moveToFirst();

在访问任何数据之前,然后执行您想要的其余操作。在您的情况下,请致电c.moveToFirst(),而不是getView()但在外面,因为您将获得所有相同的内容,因为在执行填充时会多次调用getView()。也许,在你返回光标的方法中调用它。

当您进行访问时,不要忘记将光标移动到下一个数据c.moveToNext();,您必须在getView();

中执行此操作

答案 1 :(得分:0)

我没有一个好的答案,可以使用CursorAdapter的设置方式。

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

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

编辑:  Same question as this.