0

我要疯了,想弄清楚这一点。我已经尝试了我所知道的一切,但似乎没有任何效果。非常感谢您在正确方向上的任何帮助。

当在同一个活动中按下按钮启动 ProgressDialog(在 AsyncTask 的 onPreExecute() 方法中)以更新包含要在 GalleryView 中显示的项目的字符串数组时,GalleryView 不会刷新。在 AsyncTask 的 onPostExecute() 中,我尝试在我的适配器上调用 notifyDataSetChanged(),然后在 GalleryView 上调用 setAdapter()。我相信 onPostExecute() 在 UI 线程上工作。另外,我尝试在 runOnUiThread() 中调用 notifyDataSetChanged() 和 setAdapter() ,但这似乎也不起作用。GalleryView 不会自动刷新新项目。确实如此,只有当我将视图滚动到屏幕之外然后再次返回时。只有这样,视图才会使用新项目更新。

以下是部分代码:

在按钮单击事件上,在扩展 AsyncTask 的 UpdateGalleryview 上调用执行

refresh.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {

            UpdateGalleryview updateGalleryview=new UpdateGalleryview();
            updateGalleryview.execute(GalleryView.this);
     }
});

然后在 UpdateGalleryview 类的 onPreExecute() 方法中,我正在创建一个 ProgressDialog 并更新发送到 GalleryView 适配器的数组。

private class UpdateGalleryview extends AsyncTask<Context, Integer, String>
{   

@Override
 protected void onPreExecute() 
 {
     final ProgressDialog dialog = ProgressDialog.show(GalleryView.this, "Refresh", "Loading... please wait", true);

     new Thread(new Runnable(){
        public void run(){

         //Updating the Uri[] that is sent to the Adapter for GalleryView
             File[] imagelist;
             File images = new File("/sdcard/thumbs/");
            imagelist = images.listFiles();

            mFiles = new String[imagelist.length];

            for(int i= 0 ; i< imagelist.length; i++){

                   mFiles[i] = imagelist[i].getAbsolutePath();

            }

            mUrls = new Uri[mFiles.length];

        for(int i=0; i < mFiles.length; i++){

            mUrls[i] = Uri.parse(mFiles[i]); 

        }

         dialog.dismiss();
    }

    }).start();

     Log.i( TAG, "onPreExecute()" );
     super.onPreExecute();

 }

然后,在 AsyncTask 的 onPostExecute() 中,我正在调用另一个在 UI 上运行的线程

@Override
 protected void onPostExecute( String result ) 
 {
        runOnUiThread(updateAdapter); 
//          gaAdapter.notifyDataSetChanged();

//          ga.setAdapter(gaAdapter);
//          ga.invalidate();
//          ga.setSelection(midposition);

         super.onPostExecute(result);

 }

正如您在上面看到的,我什至尝试在 onPostExecute() 本身中使用新项目和 GalleryView (ga) 更新适配器 (gaAdapter)。但这没有用。所以我创建了新线程来做这件事,如下所示。

private Runnable updateAdapter = new Runnable() { 
        @Override 
        public void run() { 
                // TODO Auto-generated method stub 
            gaAdapter.notifyDataSetChanged();

            ga.setAdapter(gaAdapter);
//                    ga.invalidate();
            ga.setSelection(midposition);
        } 
}; 

这是我的适配器代码

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {

        return imagelist.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);

        iv.setImageURI(mUrls[arg0]);

        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        iv.setLayoutParams(new Gallery.LayoutParams(180,144));
        iv.setBackgroundResource(imageBackground);

        return iv;
    }

}

这就是我所在的地方。GalleryView 不会通过执行上述操作自动刷新。

4

1 回答 1

3

您应该在第一次创建适配器时更新您使用的阵列。您可能需要在课堂上保留对它的引用。更新该数组,然后调用notifyDataSetChanged.

于 2011-11-16T20:40:33.373 回答