Reputation: 619
I am going crazy trying to figure this out. I have tried everything I knew and nothing seem to be working. Any help in the right direction is much appreciated.
GalleryView does not refresh when in the same activity I press a button to start a ProgressDialog (in onPreExecute() method of AsyncTask) to update the String array that holds the items to be displayed in the GalleryView. In onPostExecute() of AsyncTask, I tried to call notifyDataSetChanged() on my adapter followed by setAdapter() on the GalleryView. onPostExecute(), I believe, works on the UI thread. Also, I tried calling notifyDataSetChanged() and setAdapter() in runOnUiThread() but that doesn't seem to work either. GalleryView does not refresh automatically with new items. It does, only when I scroll the view out of the screen and then back in again. The view updates with the new item only then.
Below is the code in parts:
On button click event, calling execute on UpdateGalleryview that extends AsyncTask
refresh.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
UpdateGalleryview updateGalleryview=new UpdateGalleryview();
updateGalleryview.execute(GalleryView.this);
}
});
Then in onPreExecute() method of UpdateGalleryview class, I am creating a ProgressDialog and updating the array that is sent to the Adapter of 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();
}
Then, in onPostExecute() of AsyncTask, I am calling another thread that runs on UI
@Override
protected void onPostExecute( String result )
{
runOnUiThread(updateAdapter);
// gaAdapter.notifyDataSetChanged();
// ga.setAdapter(gaAdapter);
// ga.invalidate();
// ga.setSelection(midposition);
super.onPostExecute(result);
}
As you can see above, I even tried to update the Adapter (gaAdapter) with new items and the GalleryView (ga) within onPostExecute() itself. But that didn't work. So I created the new thread to do it, as below.
private Runnable updateAdapter = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
gaAdapter.notifyDataSetChanged();
ga.setAdapter(gaAdapter);
// ga.invalidate();
ga.setSelection(midposition);
}
};
This is my Adapter code
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;
}
}
This is where I am at. GalleryView does not refresh automatically by doing the above.
Upvotes: 0
Views: 1393
Reputation: 24667
You should be updating the array you use when you create the adapter the first time. You'll probably need to keep a reference to it in your class. Update that array, and then call notifyDataSetChanged
.
Upvotes: 3