I have an Activity
with GridView
, with images in it. The images are parsed from the SD card of the device. Once the Activity
starts, images are downloaded to the SD card using AsyncTask
.
The problem is that this GridView
does not get updated when images are downloaded. I want this GridView
to be updated if more images are added to the directory (i.e. if more images are downloaded). I have tried notifyDataSetChanged()
and invalidateViews()
, however they are not helping.
Here is my code for the MainActivity, Image adapter and AsyncTask
. Please try to show me where I made the mistake.
MainActivity:
ImageAdapter ia = new ImageAdapter(this);
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
ImageAdapter iAdapter = new ImageAdapter(this);
gridView.setAdapter(iAdapter);
iAdapter.notifyDataSetChanged();
gridView.invalidateViews();
new ImageDownlader(AndroidGridLayoutActivity.this, this).execute(downloadUrl);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
getView()
of Image Adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(getImageByIndex(imageThumbnails, position));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(155, 155));
return imageView;
}
ImageDownlader:
class ImageDownlader extends AsyncTask<String, Void, Void> {
private Context context;
public ImageDownlader(Context c){
context = c;
}
Sync s = new Sync();
@Override
protected Void doInBackground(String... param) {
// this is where images are downloaded to SD card
saveToSD ();
return null;
}
@Override
protected void onPreExecute() {
Log.i("Async-Example", "onPreExecute Called");
}
protected void onPostExecute()
{
Log.i("Async-Example", "onPostExecute Called");
}