【发布时间】:2014-08-01 05:37:57
【问题描述】:
我做了一个android 活动,我正在解析来自JSON webservice 的一些数据并将其显示为ListView,现在我的问题是在我的网络服务中,每个 jsonObject 都有字符串数组(图像 url) ,所以我不知道如何分别获取每个arrayList,我已经尝试过但将所有Arrayvalues放入一个arrayList,请帮助我如何在android中为每个json对象做单独的数组,我的代码和webservice如下:
webservice link
适配器
package com.eps.blancatours.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.eps.blancatours.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class DestinationAdapter extends BaseAdapter {
public ArrayList<HashMap<String, Object>> detArray;
private Context mContext;
private DisplayImageOptions options;
public static ImageLoader imageLoader;
@SuppressWarnings("deprecation")
public DestinationAdapter(Context paramContext,
ArrayList<HashMap<String, Object>> list) {
this.mContext = paramContext;
this.detArray = list;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(paramContext));
options = new DisplayImageOptions.Builder().cacheOnDisc(true)
.showStubImage(R.drawable.noimage)
.showImageOnFail(R.drawable.noimage).build();
System.out.println(":::::::::::::::mY rESULTANT aRRALYlIST::::::::::::"
+ list);
}
public int getCount() {
return this.detArray.size();
}
public Object getItem(int paramInt) {
return Integer.valueOf(paramInt);
}
public long getItemId(int paramInt) {
return paramInt;
}
@SuppressWarnings({ "static-access", "unchecked" })
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext
.getSystemService("layout_inflater");
Viewholder localViewholder = null;
if (paramView == null) {
paramView = localLayoutInflater.inflate(R.layout.raw_dest,
paramViewGroup, false);
localViewholder = new Viewholder();
localViewholder.destImg = ((ImageView) paramView
.findViewById(R.id.iv_dest));
localViewholder.destName = ((TextView) paramView
.findViewById(R.id.tv_city));
paramView.setTag(localViewholder);
} else {
localViewholder = new Viewholder();
localViewholder = (Viewholder) paramView.getTag();
}
for (HashMap<String, Object> desti : detArray) {
System.out.print("Desti id : " + desti.get("id"));
System.out.print("Desti name : " + desti.get("name"));
System.out.print("Desti program : " + desti.get("program"));
System.out.print("Desti program_link : "
+ desti.get("program_link"));
System.out.print("Desti images_array size : "
+ ((ArrayList<String>) desti.get("images_array")).size());
ArrayList<HashMap<String, Object>> hotels = (ArrayList<HashMap<String, Object>>) desti
.get("hotels");
for (HashMap<String, Object> hotel : hotels) {
System.out.print("hotel id : " + hotel.get("id"));
System.out.print("hotel city_id : " + hotel.get("city_id"));
System.out.print("hotel name : " + hotel.get("name"));
System.out.print("hotel images_array size : "
+ ((ArrayList<String>) hotel.get("images_array"))
.size());
}
localViewholder.destName.setText(detArray.get(paramInt).desti
.get("name"));
imageLoader.displayImage(detArray.get(paramInt).get(0),
localViewholder.destImg, options);
return paramView;
}
}
static class Viewholder {
ImageView destImg;
TextView destName;
}
}
代码
ListView lv_dest;
DestinationAdapter adapter;
private ProgressDialog pDialog;
JSONObject destinations = null;
JSONArray imgArray = null;
Header hdr;
JSONArray hotelArray = null;
Menu menu;
ArrayList<HashMap<String, String>> destList;
ArrayList<String> destiList, hotelList, hotel_images;
String id;
String name;
String program;
String program_link;
String hotel_id;
String city_id;
String hotel_name;
JSONArray hotel_images_array;
Intent i;
private class GetDestinations extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(DestinationsActivity.this);
pDialog.setMessage(getResources().getString(R.string.wait));
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
BackendAPIService sh = new BackendAPIService();
System.out
.println("::::::::::::::::::DESTINATIONS URL:::::::>>>>>>>>>"
+ Const.API_DESTINATIONS);
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(Const.API_DESTINATIONS,
BackendAPIService.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONArray jsonArr = new JSONArray(jsonStr);
// Getting JSON Array node
// yes
// looping through All Contacts
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject c = jsonArr.getJSONObject(i);
id = c.getString("id");
name = c.getString("name");
program = c.getString("program");
program_link = c.getString("program_link");
imgArray = c.getJSONArray("images_array");
HashMap<String, String> Destimap = new HashMap<String, String>();
Destimap.put("id", id);
Destimap.put("name", name);
Destimap.put("program", program);
Destimap.put("program_link", program_link);
if (imgArray != null && imgArray.length() != 0) {
for (int j = 0; j < imgArray.length(); j++) {
destiList.add(imgArray.getString(j));
System.out
.println("::::::::::::::::::IMAGE URL:::::::::::::::::"
+ imgArray.getString(j));
}
}
hotelArray = c.getJSONArray("hotels");
if (hotelArray != null && hotelArray.length() != 0) {
for (int k = 0; k < hotelArray.length(); k++) {
JSONObject d = hotelArray.getJSONObject(k);
hotel_id = d.getString("id");
city_id = d.getString("city_id");
hotel_name = d.getString("name");
Destimap.put("hotel_id", hotel_id);
Destimap.put("city_id", city_id);
Destimap.put("hotel_name", hotel_name);
hotel_images_array = d
.getJSONArray("images_array");
if (hotel_images_array != null
&& hotel_images_array.length() != 0) {
for (int l = 0; l < hotel_images_array
.length(); l++) {
hotel_images.add(hotel_images_array
.getString(l));
}
}
}
}
destList.add(Destimap);
}
} 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
* */
adapter = new DestinationAdapter(DestinationsActivity.this,
destList, destiList);
lv_dest.setAdapter(adapter);
}
}
【问题讨论】:
-
能否请您从服务器 url 发布 json 响应?
-
@Haresh-ya haresh 请看我的编辑。
-
您的 json 响应无效。请再检查一遍..
-
希望我正确理解了您的问题。如果您想获得所有结果,请不要在循环中创建新的数组列表。之前创建它并继续将东西添加到同一个arrayList中。
-
@Sweety-Wait 我正在编辑我的代码并告诉你,请告诉我它是否正确?
标签: java android json web-services arraylist