【问题标题】:Why does the GalleryView not refresh with new items?为什么 GalleryView 不刷新新项目?
【发布时间】:2011-11-16 20:31:03
【问题描述】:

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

在同一个活动中,我按下按钮启动 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 不会自动刷新。

【问题讨论】:

  • 如果您发布代码,可能会更容易帮助您。
  • 我已经用代码编辑了我的帖子。

标签: android android-asynctask galleryview


【解决方案1】:

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

【讨论】:

  • yourAdapter.notifyDataSetChanged() 会做到的。
  • 是的,但他更新的数组应该是传递给该适配器的数组,而不是新数组。我认为这可能是没有看到任何代码的问题。
  • 我正在更新同一个数组,而不是创建一个新数组。我会用一些代码编辑我的帖子。
  • 我查看了您发布的代码。仍然不清楚您是否正在更新适配器使用的数组。将代码发布到您创建画廊适配器的位置。
  • 同意@C0deAttack,我们无法判断您是否真的在更新适配器中的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多