我在我的应用程序中使用json解析。我有ListView
显示一些数据和ImageView
。这里我想显示来自ImageView
中另一个网址的图片。怎么做?
这是我的代码......
private ProgressDialog pDialog;
private static String url = "http://myurl/json/main.json";
// JSON Node names
private static final String TAG_NAME = "name";
private static final String TAG_VIEWERS = "viewers";
JSONArray myarray = null;
ArrayList<HashMap<String, String>> mylist;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView bmImage = (ImageView) findViewById(R.id.img);
mylist = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
}
});
new GetMyarray().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetMyarray extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Processing...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject getResponse = new JSONObject(jsonStr);
Log.d("Response: ", "> " + jsonStr);
JSONObject pnames=getResponse.getJSONObject("getnames");
Log.d("Response: ", "> " + pnames);
myarray = pnames.getJSONArray("pnames");
Log.d("Listed as: ", "> " + myarray);
for (int i = 0; i < myarray.length(); i++) {
JSONObject c = myarray.getJSONObject(i);
String name = c.getString(TAG_NAME);
String viewers=c.getString(TAG_VIEWERS);
HashMap<String, String> myarray = new HashMap<String, String>();
myarray.put(TAG_NAME, name);
myarray.put(TAG_VIEWERS, viewers);
mylist.add(myarray);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, mylist,
R.layout.list_item, new String[] { TAG_NAME,TAG_VIEWERS }, new int[] { R.id.name,R.id.viewers
});
setListAdapter(adapter);
}
}
}
答案 0 :(得分:0)
Here它展示了如何从URL
加载listview中的图像只是为了从URL加载图像,有很多SO线程
答案 1 :(得分:0)
请访问
用于延迟加载实现的listview
https://github.com/square/picasso
答案 2 :(得分:0)
尝试这种方法
URL url = new URL("your url");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
和
public boolean loadImageFromURL(String url, ImageView iv){
try {
URL myUrl = new URL (url);
HttpURLConnection conn =
(HttpURLConnection) myUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
iv.setImageBitmap(BitmapFactory.decodeStream(is));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
* 记住:在Asyntask中执行这两个方法。否则会崩溃