MKD*_*nys 0 android universal-image-loader picasso
从互联网文本数据加载并链接到图像.我无法理解如何在listview中加载图像.阅读,它需要使用毕加索或通用图像加载器,但不明白如何.请更正我的代码
public class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getActivity(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
private final String ATTRIBUTE1 = "header";
private final String ATTRIBUTE2 = "short_text";
private final String ATTRIBUTE3 = "team";
private final String ATTRIBUTE4 = "datatime";
private final String ATTRIBUTE5 = "photo_url";
Drawable drawable;
// build hash set for list view
public void ListDrwaer() {
// ??????????? ?????? ? ???????? ??? ???????? ?????????
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> m;
// ?????? ???? ?????????, ?? ??????? ????? ???????? ??????
String[] from = {ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3, ATTRIBUTE4, ATTRIBUTE5};
// ?????? ID View-???????????, ? ??????? ????? ????????? ??????
int[] to = { R.id.header, R.id.short_text, R.id.team, R.id.datatime, R.id.img_news};
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("news");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String header = jsonChildNode.optString("header");
String short_text = jsonChildNode.optString("short_text");
String team = jsonChildNode.optString("team");
String datatime = jsonChildNode.optString("datatime");
String photo_url = jsonChildNode.optString("photo_url");
// ??????? ????? Map
m = new HashMap<String, Object>();
m.put(ATTRIBUTE1, header);
m.put(ATTRIBUTE2, short_text);
m.put(ATTRIBUTE3, team);
m.put(ATTRIBUTE4, datatime);
// m.put(ATTRIBUTE5, url_photo);
// ????????? ??? ? ?????????
data.add(m);
}
} catch (JSONException e) {
Toast.makeText(getActivity(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), data,
R.layout.news_details,
from, to);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(simpleAdapter);
listView.onRestoreInstanceState(state);
}
Run Code Online (Sandbox Code Playgroud)
}
创建自己的适配器,从ArrayAdapter扩展.有很多例子:
然后在你的getView方法调用(类似的东西取决于你的实现):
Picasso.with(context).load(getItem(position).get(ATTRIBUTE5)).into(holder.imageView);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅Picasso 文档.
| 归档时间: |
|
| 查看次数: |
5843 次 |
| 最近记录: |