title: Dynamically refresh gridview when image replaced. notifyDataSetChanged() not working
body: <p>I have a <code>GridView</code> that loads images from SD card (if present) otherwise some default images. Once images are loaded, the user can select any image cell and take a new picture. When default pictures are loaded in <code>GridView</code> and user takes a picture, the <code>GridView</code> updates and shows the image - but if user clicks the cell again and takes a new picture, then <code>GridView</code> is not updated. Only when I kill the app and restart it, then the new picture appears.</p>
  
  <p>I have tried <code>notifyDataSetChanged()</code> but it doesn't work when image is refreshed. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class Fragment1 extends SherlockFragment {
  ...
  private Integer[] mThumbIds = { R.drawable.fr, R.drawable.siderelaxed3,
          R.drawable.br, R.drawable.fdbth, R.drawable.bdb, R.drawable.bls,
          R.drawable.fls, R.drawable.mm };
  
  
  private ImageAdapter imageAdapter;
  ArrayList&lt;String&gt; f = new ArrayList&lt;String&gt;();// list of file paths
  File[] listFile;
  ImageLoader imageLoader;    
  Context context;
  boolean needToRefresh = true;
  
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      context = getActivity();
      date = util.getCurrentDate();
      Bundle bundle = this.getArguments();
      date = bundle.getString(MyCommons.SELECTED_DATE);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View mainView = inflater.inflate(R.layout.picturerecord, container,
              false);
  
      getFromSdcard();
      Grid = (GridView) mainView
              .findViewById(R.id.pictureRecordGrid);
      imageAdapter = new ImageAdapter();
      Grid.setAdapter(imageAdapter);
      imageLoader = new ImageLoader(context, 4);
  
      try {
          ourClass = Class
                  .forName("cameraOpen");
          ourIntent = new Intent(context, ourClass);
  
      } catch (ClassNotFoundException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
      poseGrid.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView&lt;?&gt; parent, View v,
                  int position, long id) {
              startActivityForResult(ourIntent, CAMERA_REQUEST);
              poseSelected = position;
          }
      });     
      return mainView;
  
  }
  
  public void getFromSdcard() {
      f.clear();
      for (int  i = 0; i &lt; MyCommons.POSES_TO_LOAD.length; i++) {
          String pose = MyCommons.pose_names[i];
          String path = MyCommons.rootPath + File.separator + pose
                  + File.separator + date;
          File imgFile = new File(path);
          if (imgFile.exists()) {
              f.add(path);
              Log.d(TAG, "image is present at:" + path);
          } else {
              f.add("loaddefault");
          }
      }
  }
  public class ImageAdapter extends BaseAdapter {
      private LayoutInflater mInflater;
  
      public ImageAdapter() {
          mInflater = (LayoutInflater) context.getSystemService(
                  Context.LAYOUT_INFLATER_SERVICE);
      }
  
      public int getCount() {
          return f.size();
      }
  
      public Object getItem(int position) {
          return position;
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (needToRefresh == true) {
              convertView = null;
              Log.d(TAG, "NEEDED REFRESH");
              needToRefresh = false;
          }
          if (convertView == null) {
              holder = new ViewHolder();
              convertView = mInflater.inflate(R.layout.picturerecorddata,
                      null);
              holder.imageview = (ImageView) convertView
                      .findViewById(R.id.pictureRecordImage);
              holder.textview = (TextView) convertView.findViewById(R.id.pictureRecordText);
  
              convertView.setTag(holder);
  
          } else {
              holder = (ViewHolder) convertView.getTag();             
          }
  
          if (f.get(position).equals("loaddefault")) {
              DefaultImageLoader defaultImages = new DefaultImageLoader(holder.imageview);
              defaultImages.execute(position);
              } else {
              imageLoader.DisplayImage(f.get(position), holder.imageview, mThumbIds[position], position);
          }
  
          holder.textview.setText(MyCommons.pose_names[position]);
          return convertView;
      }
  }
  
  class ViewHolder {
      ImageView imageview;
      TextView textview;
  
  }
  
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == CAMERA_REQUEST) {
          if (resultCode == Activity.RESULT_OK) {
              Bitmap photo = BitmapFactory.decodeByteArray(
                      data.getByteArrayExtra("BitmapImage"), 0,
                      data.getByteArrayExtra("BitmapImage").length);
  
              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  
  
              File root = new File(MyCommons.rootPath);
  
              if (!root.exists()) {
                  root.mkdirs();
                  Log.d(TAG, "Created Progress pics directory.");
              }
  
              String poseName = MyCommons.pose_names[poseSelected];
  
              File pose_dir = new File(root + File.separator + poseName);
              if (!pose_dir.exists()) {
                  pose_dir.mkdirs();
                  Log.d(TAG, "Created" + pose_dir + " pics directory.");
              }
              String tempFileName = root + File.separator + poseName + File.separator + date;
              File tempFile = new File(tempFileName);
              if (tempFile.exists()) {
                  Log.d(TAG, "File already exist. deleteing it and will write new file:" + tempFileName);
  
                  tempFile.delete();
              }
              util.saveImage(bytes, pose_dir, date);
  
              Log.d(TAG, "image saved. Reloading adapter:\n"  + tempFileName);
              f.remove(tempFileName);
  
              getFromSdcard();                
              f.add(poseSelected, tempFileName);  
                  //f.clear();                
              //f.addAll(f);              
              imageAdapter.notifyDataSetChanged();
  
              Grid.invalidateViews();
              Grid.setAdapter(imageAdapter);                          
          }
      }
  }
  ...
  </code></pre>
  
  <p>}</p>