title: Android how to refresh image grid view adapter on tab change/swipe?
body: <p>I am new to native android development. I have 3 tabs (Map, Camera, Pictures). <code>Map</code>is displaying my current gps location with marker on map. <code>Camera</code> is used to take picture and <code>Pictures</code> have a grid view to show saved pictures taken from camera. The screen look like below </p>
  
  <p><a href="https://i.stack.imgur.com/LhUDp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LhUDp.png" alt="enter image description here"></a></p>
  
  <p>Now when i take picture from the camera (in second tab) and then go to the 3rd tab the picture is not there, but when i close the app and then go to the <code>pictures</code> tab the picture is shown. I want to refresh the <code>image adapter</code> when swiping to the <code>picture</code> tab. For this i searched many articles and found that <code>notifyDataSetChanged()</code> is used for this purpose, so i tried to implement it on my <code>picture</code> class but it doesn't refresh it. </p>
  
  <p>Below is my adapter code </p>
  
  <pre><code>public class GridViewImageAdapter extends BaseAdapter {
  
  private Activity _activity;
  private ArrayList&lt;String&gt; _filePaths = new ArrayList&lt;String&gt;();
  private int imageWidth;
  
  public GridViewImageAdapter(Activity activity, ArrayList&lt;String&gt; filePaths,
                              int imageWidth) {
      this._activity = activity;
      this._filePaths = filePaths;
      this.imageWidth = imageWidth;
  }
  
  @Override
  public int getCount() {
      return this._filePaths.size();
  }
  
  @Override
  public Object getItem(int position) {
      return this._filePaths.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) {
          imageView = new ImageView(_activity);
      } else {
          imageView = (ImageView) convertView;
      }
  
      // get screen dimensions
      Bitmap image = decodeFile(_filePaths.get(position), imageWidth,
              imageWidth);
  
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
              imageWidth));
      imageView.setImageBitmap(image);
  
      // image view click listener
      imageView.setOnClickListener(new OnImageClickListener(position));
  
      return imageView;
  }
  
  class OnImageClickListener implements OnClickListener {
  
      int _postion;
  
      // constructor
      public OnImageClickListener(int position) {
          this._postion = position;
      }
  
      @Override
      public void onClick(View v) {
          // on selecting grid view image
          // launch full screen activity
          /*Intent i = new Intent(_activity, FullScreenViewActivity.class);
          i.putExtra("position", _postion);
          _activity.startActivity(i);*/
      }
  
  }
  
  /*
   * Resizing image size
   */
  public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
      try {
  
          File f = new File(filePath);
  
          BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(new FileInputStream(f), null, o);
  
          final int REQUIRED_WIDTH = WIDTH;
          final int REQUIRED_HIGHT = HIGHT;
          int scale = 1;
          while (o.outWidth / scale / 2 &gt;= REQUIRED_WIDTH
                  &amp;&amp; o.outHeight / scale / 2 &gt;= REQUIRED_HIGHT)
              scale *= 2;
  
          BitmapFactory.Options o2 = new BitmapFactory.Options();
          o2.inSampleSize = scale;
          return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
      return null;
  }}
  </code></pre>
  
  <p>Below is my code for <code>picture</code></p>
  
  <pre><code>public class Pictures extends Fragment {
  
  private Utils utils;
  private ArrayList&lt;String&gt; imagePaths = new ArrayList&lt;String&gt;();
  private GridViewImageAdapter adapter;
  private GridView gridView;
  private int columnWidth;
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.pictures, container, false);
  
      gridView = (GridView) rootView.findViewById(R.id.grid_view);
      utils = new Utils(getContext());
  
      // Initilizing Grid View
      InitilizeGridLayout();
  
      // loading all image paths from SD card
      imagePaths = utils.getFilePaths();
  
      // Gridview adapter
  
      adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
      /*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
              columnWidth);*/
  
      // setting grid view adapter
      gridView.setAdapter(adapter);
  
     // called the method here
      adapter.notifyDataSetChanged();
  
  
      return rootView;
  }
  
  private void InitilizeGridLayout() {
  
      Resources r = getResources();
      float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
              AppConstant.GRID_PADDING, r.getDisplayMetrics());
  
      columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
  
      gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
      gridView.setColumnWidth(columnWidth);
      gridView.setStretchMode(GridView.NO_STRETCH);
      gridView.setPadding((int) padding, (int) padding, (int) padding,
              (int) padding);
      gridView.setHorizontalSpacing((int) padding);
      gridView.setVerticalSpacing((int) padding);
  }}
  </code></pre>
  
  <p><strong>Update 1</strong></p>
  
  <p>Below is my code for <code>FragmentPagerAdapter</code> which is in my <code>MainActivity</code></p>
  
  <pre><code>public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  MyLocation loc = new MyLocation();
                  return loc;
              case 1:
                  Camera cam= new Camera();
                  return cam;
             case 2:
                  Pictures pic = new Pictures();
                  return pic;
  
              default:
                  return null;
          }
  
      }
  
      @Override
      public int getCount() {
          // Show 3 total pages.
          return 3;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch (position) {
              case 0:
                  return "MyLocation";
              case 1:
                  return "Camera";
             case 2:
                  return "Pictures";
          }
          return null;
      }
  }
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>I have followed the suggestion of <code>Valentino S.</code> but still unable to view the image capture from the camera in second tab. </p>
  
  <p>Below is the updated code </p>
  
  <p>**MainActivity.java **</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
    ........
            public interface Updateable
            {
                  public void update();
            }
  
   public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public int getItemPosition(Object object)
      {
          if(object != null &amp;&amp; object instanceof Pictures)
          {
              Pictures p = (Pictures) object;
              p.update();
          }
          return super.getItemPosition(object);
      }
  
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  MyLocation loc = new MyLocation();
                  return loc;
              case 1:
                  Camera cam= new Camera();
                  return cam;
             case 2:
                  Pictures pic = new Pictures();
                  return pic;
  
              default:
                  return null;
          }
  
      }
  
      @Override
      public int getCount() {
          // Show 3 total pages.
          return 3;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          switch (position) {
              case 0:
                  return "MyLocation";
              case 1:
                  return "Camera";
             case 2:
                  return "Pictures";
          }
          return null;
       }// end charsequence
  
    }// end SectionsPagerAdapter 
  
  }// end mainactivity
  </code></pre>
  
  <p><strong>Pictures.java</strong></p>
  
  <pre><code>public class Pictures extends Fragment implements MainActivity.Updateable {
  ........
   @Override
  public void update() {
      if(adapter !=null)
      {
          adapter.notifyDataSetChanged();
  
      }
      else
      {
          Log.d(getTag(),"null");
      }
    }
  .......
  }
  </code></pre>
  
  <p>So when i run the app, take the picture from the camera and then go to <code>Pictures</code> tab i won't see my picture. I am still facing the same issue.</p>
  
  <p>Moreover i have debugged my code, it never goes to the <code>update</code> and <code>getItemPostion</code> method and that worries me the most.</p>
  
  <p>I am stuck to it and really don't have a clue how to do it. 
  Any help would be highly appreciated</p>