在我的 android 应用程序中,我从 url 获取图像并将其显示在 Grid View 中。但是当我向下滚动时,图像会随机改变位置。我正在使用异步线程来获取图像并更新图像视图对象。任何建议如何解决这个问题......
我已经提到了gridview中的Images change their position on scrolling in android的问题,但这对我没有帮助
My code for getView and loading the image
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final HashMap<String, String> song = songsList.get(position);
imageURL = song.get(VariablesList.TAG_ALBUM_IMAGE);
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.gridlayout_item, parent, false);
final ImageView imageView = (ImageView) v.findViewById(R.id.icon_image);
new LoadImage().execute(imageURL,imageView);
imageView.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View v) {
albumUrl = new StringBuffer(resources.getString(R.string.songsListURL));
String albumIndex = song.get("id");
albumName = (song.get("name"));
imageURL = song.get(VariablesList.TAG_ALBUM_IMAGE);
SongsList albumList = new SongsList(imageURL, albumUrl,
albumName,albumIndex,resources);
Thread threadAlbumList = new Thread(albumList);
threadAlbumList.start();
synchronized (albumList) {
try {
albumList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!NewMediaPlayer.mediaPlayer.isPlaying()) {
HashMap<String, String> playingSong = NewMediaPlayer.selectedSongs
.get(0);
if (playingSong != null) {
String url = playingSong.get("songUrl");
String songName = playingSong.get("songName");
if (songName != null)
{
NewMediaPlayer.songTitleLabel.setText(albumName
+ " - " + songName);
NewMediaPlayer.songTitle.setText(albumName+"-"+songName);
}
NewMediaPlayer.playSong(url);
}
}
}
});
TextView itemAlbumName = (TextView) v.findViewById(R.id.icon_text);
itemAlbumName.setText(song.get("name"));
itemAlbumName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HashMap<String, String> song = songsList.get(position);
String songIndex = song.get("id");
String albumName = (song.get("name"));
Intent in = new Intent(context, SongListActivity.class);
in.putExtra("albumIndex", songIndex);
in.putExtra("albumName", albumName);
in.putExtra("AlbumImage", song.get(VariablesList.TAG_ALBUM_IMAGE));
context.startActivity(in);
}
});
return v;
}
public void LoadImage extends AsyncTask<Object, Void, Bitmap>{
ImageView imageView;
@Override
protected BitMap doInBackground(Object... urls) {
String imageURL = (String)urls[0];
imageView = (ImageView)urls[0];
BitMap image = AlbumList.LoadImagetoGridView(imageURL)
return image
}
@Override
protected void onPostExecute(BitMap result) {
imageView.setImageBitmap(result);
}
}