我有一个ListView
,每个项目都有一张图片。我从服务器下载图像。由于有两个以上的图像,加载变得非常慢,当您向下滚动时,您会看到上面的图像,并且必须等待很长时间才能更改为正确的图像。
我已经尝试使用此帖子中的提示:http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
但它仍然很慢。什么是改善这一点的最好和最简单的方法。
以下是我BaseAdapter
班级的代码:
public class GetAllEntrysListViewAdapter extends BaseAdapter {
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
public Cursor cursor;
private SQLiteDatabase dbase;
DbHelper dbh;
private Context context;
String pos;
Integer markerID;
public String objectID;
private static final String baseUrlForImage = "http://...";
public GetAllEntrysListViewAdapter(JSONArray jsonArray, Context context, Integer markerID) {
this.dataArray = jsonArray;
this.context= context;
this.markerID = markerID;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ListCell cell;
if (convertView == null) {
convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);
cell = new ListCell();
cell.likes = (TextView) convertView.findViewById(R.id.listViewLikes);
cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
cell.likeImage = (ImageView) convertView.findViewById(R.id.heartImage);
convertView.setTag(cell);
}
else {
cell = (ListCell)convertView.getTag();
}
cell.position = position;
try {
JSONObject jsonObject = this.dataArray.getJSONObject(position);
cell.likes.setText(jsonObject.getString("likes"));
cell.note.setText(jsonObject.getString("note"));
cell.entryID = jsonObject.getString("id");
String img = jsonObject.getString("image");
String urlForImageInServer = baseUrlForImage + img;
new AsyncTask<String, Void, Bitmap>() {
private int mPosition = position;
private ListCell mCell = cell;
@Override
protected Bitmap doInBackground(String... params) {
//download image
String url = params[0];
Bitmap icon = null;
try {
InputStream in = new java.net.URL(url).openStream();
icon = BitmapFactory.decodeStream(in);
} catch(MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return icon;
}
@Override
protected void onPostExecute(Bitmap result) {
if (cell.position == mPosition) {
cell.img.setImageBitmap(result);
}
}
}.execute(urlForImageInServer);
catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}
public static class ListCell {
private TextView likes;
private TextView note;
private ImageView img;
public ImageView likeImage;
public int position;
public String entryID;
}
}
答案 0 :(得分:2)
不要重新发明轮子。不要为AsyncTask
的每个项目创建ListView
。使用一些库,它将下载,缓存并帮助显示您的图像,如Picasso:
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
对于JSONObject
的每个项目,将ListView
拆分为单独的参数是多余的。创建POJO
个对象的数组,但不只是JSONObject
的数组。或者,如果您的ListView
仅显示图片,则可以将String
数组传递给适配器。
PS
并尝试查找和阅读新文章。你提到了2012
年的文章。
答案 1 :(得分:0)
到目前为止,最受欢迎的SO帖子是@ Lazy load of images in ListView。它提到了毕加索以及其他包装。 玩得开心......
答案 2 :(得分:0)
你可以使用Volley Library它会让你放松并做一切
http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
var users = Users.Instance;
users.addNewUser(1, "Vera");
users.countUsers();