我正在尝试编写新闻列表应用程序。我从服务器解析了JSON数据并创建了CustomListAdapter。我的listView中有三个textView和imageView。它运行良好,图像加载,我将其设置为imageView。从服务器重新加载滚动列表视图图像时。这是我的片段:
String
这是我的自定义ListView适配器:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.news_fragment, container, false);
this.drawerPosition = getArguments().getInt(ARG_PLANET_NUMBER);
lv = (ListView) rootView.findViewById(R.id.listView);
View v = inflater.inflate(R.layout.list_footer, null);
lv.addFooterView(v);
isLoading = true;
Http.getLists(aContext, this.drawerPosition, pageNumber, resultHandler);
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int l = visibleItemCount + firstVisibleItem;
if (l >= totalItemCount && !isLoading) {
// It is time to add new data. We call the listener
isLoading = true;
Http.getLists(aContext, drawerPosition, pageNumber, appendHandler);
}
}
});
return rootView;
}
public void setListview() {
listAdapter = new ListViewCustomAdapter(aContext, arrayList);
lv.setAdapter(listAdapter);
}
AsyncHttpResponseHandler resultHandler = new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode != 200)
return;
try {
JSONObject json = new JSONObject(new String(responseBody));
Log.d("JSON Result: ", json.toString());
String result = json.getString("status");
if (result.equalsIgnoreCase("complete")) {
String pagenum = json.getString("pagenum");
JSONArray items = json.getJSONArray("items");
ArrayList<NewsList> s = new ArrayList<NewsList>();
for (int i = 0; i < items.length(); i++) {
s.add(new NewsList(
Integer.parseInt(items.getJSONObject(i).getString("ID")),
items.getJSONObject(i).getString("post_date"),
items.getJSONObject(i).getString("category"),
items.getJSONObject(i).getString("post_title"),
items.getJSONObject(i).getString("image")
));
}
arrayList = s;
isLoading = false;
pageNumber = Integer.valueOf(pagenum) + 1;
setListview();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
String answ = error.getLocalizedMessage();
Log.d("API", answ);
}
};
答案 0 :(得分:1)
尝试下面的代码,它可以帮助您解决问题。
public class ListViewCustomAdapter extends..{
//Map to cache Image Bitmap. Key= imageUrl,value =Image Bitmap
private Map<String, Bitmap> mBitmapCache = new HashMap<String, Bitmap>();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.nTitle = (TextView) convertView.findViewById(R.id.listTitle);
holder.nDate = (TextView) convertView.findViewById(R.id.listDate);
holder.nCategory = (TextView) convertView.findViewById(R.id.listCategory);
holder.nImage = (ImageView) convertView.findViewById(R.id.listImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (newsList.get(position) != null) {
holder.imageUrl = newsList.get(position).image;
holder.nTitle.setText(newsList.get(position).title);
holder.nCategory.setText(newsList.get(position).category);
holder.nDate.setText(Base.getInstance(context).getDateString(newsList.get(position).date));
if (holder.imageUrl != null && !holder.imageUrl.equals("null")) {
String thumbUrl = holder.imageUrl.substring(0, holder.imageUrl.lastIndexOf('.')) + "-260x145" + holder.imageUrl.substring(holder.imageUrl.lastIndexOf('.'), holder.imageUrl.length());
holder.setImage(thumbUrl);
}
}
return convertView;
}
private static class ViewHolder {
TextView nTitle;
TextView nDate;
TextView nCategory;
ImageView nImage;
String imageUrl;
public void setImage(String imageUrl) {
this.imageUrl = imageUrl;
Bitmap imageBitmap = mBitmapCache.get(imageUrl);
if(imageBitmap!=null){
nImage.setImageBitmap(imageBitmap);
} else {
AsyncHttpClient client = new AsyncHttpClient();
client.get(imageUrl, null, fileHandler);
}
}
FileAsyncHttpResponseHandler fileHandler = new FileAsyncHttpResponseHandler(context) {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable,
File response) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, File response) {
Bitmap imageBitmap = BitmapFactory.decodeFile(response.getPath());
imageView.setImageBitmap(imageBitmap);
mBitmapCache.put(imageUrl, imageBitmap);
}
};
}
}
答案 1 :(得分:0)
您的代码
public void setImage() {
if (imageUrl != null && !imageUrl.equals("null") && !imageUrl.equals("")) {
AsyncHttpClient client = new AsyncHttpClient();
client.get(imageUrl, null, fileHandler);
}
}
看起来很可疑:任何有效的网址都会强制重新加载,因为它符合
中的所有三个条件 imageUrl != null && !imageUrl.equals("null") && !imageUrl.equals("")
实施例: http://www.google.com/logo.png不为空,不等于字符串&#34; null&#34;也不是字符串&#34;&#34;,因此if将始终为true并且AsyncLoader已创建。 顺便说一句,检查&#34; null&#34;只有在代码中的其他地方明确使用它时才有意义。很可能你只想检查空值,而不是&#34; null&#34;字符串。