Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the GalleryView not refresh with new items?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Below is the code in parts:</p> <p>On button click event, calling execute on UpdateGalleryview that extends AsyncTask</p> <pre><code>refresh.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { UpdateGalleryview updateGalleryview=new UpdateGalleryview(); updateGalleryview.execute(GalleryView.this); } }); </code></pre> <p>Then in onPreExecute() method of UpdateGalleryview class, I am creating a ProgressDialog and updating the array that is sent to the Adapter of GalleryView.</p> <pre><code>private class UpdateGalleryview extends AsyncTask&lt;Context, Integer, String&gt; { @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&lt; imagelist.length; i++){ mFiles[i] = imagelist[i].getAbsolutePath(); } mUrls = new Uri[mFiles.length]; for(int i=0; i &lt; mFiles.length; i++){ mUrls[i] = Uri.parse(mFiles[i]); } dialog.dismiss(); } }).start(); Log.i( TAG, "onPreExecute()" ); super.onPreExecute(); } </code></pre> <p>Then, in onPostExecute() of AsyncTask, I am calling another thread that runs on UI</p> <pre><code>@Override protected void onPostExecute( String result ) { runOnUiThread(updateAdapter); // gaAdapter.notifyDataSetChanged(); // ga.setAdapter(gaAdapter); // ga.invalidate(); // ga.setSelection(midposition); super.onPostExecute(result); } </code></pre> <p>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.</p> <pre><code>private Runnable updateAdapter = new Runnable() { @Override public void run() { // TODO Auto-generated method stub gaAdapter.notifyDataSetChanged(); ga.setAdapter(gaAdapter); // ga.invalidate(); ga.setSelection(midposition); } }; </code></pre> <p>This is my Adapter code</p> <pre><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; } } </code></pre> <p>This is where I am at. GalleryView does not refresh automatically by doing the above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload