我正在使用AsyncTask将图像加载到我的ListView中。但是,当我运行应用程序时,滚动时图像的顺序会发生变化。我怀疑getView()方法可能有问题。但我找不到办法解决这个问题。
对Android来说相对较新,我也无法探索太多选项。
这是我的代码:
DisplayPlacements.java
package com.example.android.areainfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
public class DisplayPlacements extends AppCompatActivity {
ListView listView;
private String[] imageURLArray = new String[]{
"http://farm8.staticflickr.com/7315/9046944633_881f24c4fa_s.jpg",
"http://farm4.staticflickr.com/3777/9049174610_bf51be8a07_s.jpg",
"http://farm8.staticflickr.com/7324/9046946887_d96a28376c_s.jpg",
"http://farm3.staticflickr.com/2828/9046946983_923887b17d_s.jpg",
"http://farm4.staticflickr.com/3810/9046947167_3a51fffa0b_s.jpg",
"http://farm4.staticflickr.com/3773/9049175264_b0ea30fa75_s.jpg",
"http://farm4.staticflickr.com/3781/9046945893_f27db35c7e_s.jpg",
"http://farm6.staticflickr.com/5344/9049177018_4621cb63db_s.jpg",
"http://farm8.staticflickr.com/7307/9046947621_67e0394f7b_s.jpg",
"http://farm6.staticflickr.com/5457/9046948185_3be564ac10_s.jpg",
"http://farm4.staticflickr.com/3752/9046946459_a41fbfe614_s.jpg",
"http://farm8.staticflickr.com/7403/9046946715_85f13b91e5_s.jpg",
"http://farm8.staticflickr.com/7315/9046944633_881f24c4fa_s.jpg",
"http://farm4.staticflickr.com/3777/9049174610_bf51be8a07_s.jpg",
"http://farm8.staticflickr.com/7324/9046946887_d96a28376c_s.jpg",
"http://farm3.staticflickr.com/2828/9046946983_923887b17d_s.jpg",
"http://farm4.staticflickr.com/3810/9046947167_3a51fffa0b_s.jpg",
"http://farm4.staticflickr.com/3773/9049175264_b0ea30fa75_s.jpg",
"http://farm4.staticflickr.com/3781/9046945893_f27db35c7e_s.jpg",
"http://farm6.staticflickr.com/5344/9049177018_4621cb63db_s.jpg",
"http://farm8.staticflickr.com/7307/9046947621_67e0394f7b_s.jpg",
"http://farm6.staticflickr.com/5457/9046948185_3be564ac10_s.jpg",
"http://farm4.staticflickr.com/3752/9046946459_a41fbfe614_s.jpg",
"http://farm8.staticflickr.com/7403/9046946715_85f13b91e5_s.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_placements);
listView = (ListView) findViewById(R.id.listPlacements);
ImageAdapter imageAdapter = new ImageAdapter(this, R.layout.row, imageURLArray);
listView.setAdapter(imageAdapter);
}
}
activity_display_placements.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_display_placements"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.areainfo.DisplayPlacements">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listPlacements">
</ListView>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/testImage"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="centerCrop" />
</LinearLayout>
ImageAdapter.java
package com.example.android.areainfo;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.io.IOException;
import java.net.URL;
/**
* Created by User on 30-Jan-17.
*/
public class ImageAdapter extends ArrayAdapter<String> {
private String[] imageURLArray;
private LayoutInflater inflater;
public ImageAdapter(Context context, int textViewResourceId,
String[] imageArray) {
super(context, textViewResourceId, imageArray);
// TODO Auto-generated constructor stub
inflater = ((Activity)context).getLayoutInflater();
imageURLArray = imageArray;
}
private static class ViewHolder {
String imageURL;
ImageView imageView;
Bitmap bitmap;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
if(convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.testImage);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.imageURL = imageURLArray[position];
new DownloadAsyncTask().execute(viewHolder);
return convertView;
}
private class DownloadAsyncTask extends AsyncTask<ViewHolder, Void, ViewHolder> {
@Override
protected ViewHolder doInBackground(ViewHolder... params) {
// TODO Auto-generated method stub
//load image directly
ViewHolder viewHolder = params[0];
try {
URL imageURL = new URL(viewHolder.imageURL);
viewHolder.bitmap = BitmapFactory.decodeStream(imageURL.openStream());
} catch (IOException e) {
// TODO: handle exception
Log.e("error", "Downloading Image Failed");
viewHolder.bitmap = null;
}
return viewHolder;
}
@Override
protected void onPostExecute(ViewHolder result) {
// TODO Auto-generated method stub
if (result.bitmap == null) {
// result.imageView.setImageResource(R.drawable.postthumb_loading);
} else {
result.imageView.setImageBitmap(result.bitmap);
}
}
}
}
帮助表示赞赏。
谢谢!