我在我的应用程序中使用了视图。我想使用POST方法在列表中显示图像。我通过POST方法获取了图像网址但无法在列表中显示它。我的代码片段的某些部分就在这里......
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Home1.this);
pDialog.setMessage("Please wait....");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler1 sh = new ServiceHandler1(apikey, latitude, longitude);
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler1.POST);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String username = c.getString(TAG_USERNAME);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String contact_number = c.getString(TAG_CONTACT_NUMBER);
String postalcode = c.getString(TAG_POSTAL_CODE);
String image = c.getString(TAG_IMAGE);
Log.v("as.",image);
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
ImageView Profileimage = (ImageView) findViewById(R.id.logo);
imageLoader.DisplayImage(image, Profileimage);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_NAME, name);
contact.put(TAG_IMAGE, image);
contact.put(TAG_EMAIL, email);
// contact.put(TAG_ADDRESS, address);
contact.put(TAG_ADDRESS, address);
// adding contact to contact list
contact.put(TAG_USERNAME, username);
contact.put(TAG_POSTAL_CODE, postalcode);
contact.put(TAG_CONTACT_NUMBER, contact_number);
contactList.add(contact);
}
} 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();
ListAdapter adapter = new SimpleAdapter(
Home1.this, contactList,
R.layout.list_item1, new String[]{TAG_EMAIL, TAG_USERNAME, TAG_ADDRESS, TAG_POSTAL_CODE, TAG_CONTACT_NUMBER,
}, new int[]{
R.id.company, R.id.description, R.id.address, R.id.operating_hours, R.id.contact});
setListAdapter(adapter);
}
}
我使用图片加载器类转换图片网址...伙计们请帮帮我
请建议我如何在postExecute()...
中解析图像 @Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
Home1.this, contactList,
R.layout.list_item1, new String[]{TAG_EMAIL, TAG_USERNAME, TAG_ADDRESS, TAG_POSTAL_CODE, TAG_CONTACT_NUMBER,
}, new int[]{
R.id.company, R.id.description, R.id.address, R.id.operating_hours, R.id.contact});
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
ImageView Profileimage = (ImageView) findViewById(R.id.logo);
imageLoader.DisplayImage(TAG_IMAGE, Profileimage);
setListAdapter(adapter);
}
}
答案 0 :(得分:0)
您需要创建自定义Listview
适配器才能完成此任务。请检查loading-image-asynchronously-in-android-listview链接以创建一个。在post post方法中,你必须创建适配器对象,然后传递包含图像URL的contactList
数组列表。
在链接中,您将获得在listview
中显示图片的完整代码,其中包含使用Imageloader
库下载图片的代码。
如果有帮助,请告诉我。