我有一个Activity
with GridView
, 里面有图片。图像是从设备的 SD 卡中解析的。启动Activity
后,图像会使用AsyncTask
.
问题是GridView
下载图像时不会更新。如果将更多图像添加到目录中(即如果下载了更多图像),我希望GridView
更新它。我试过了notifyDataSetChanged()
,invalidateViews()
但是他们没有帮助。
这是我的 MainActivity、图像适配器和AsyncTask
. 请尝试告诉我我在哪里犯了错误。
主要活动:
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()
图像适配器:
@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;
}
图片下载:
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");
}