如何使用 Listview 访问多个 JSONArray?
Posted
技术标签:
【中文标题】如何使用 Listview 访问多个 JSONArray?【英文标题】:How to access Multiple JSONArrays with Listview? 【发布时间】:2016-03-14 07:16:07 【问题描述】:我有两个Listview
,我从服务器收到两个JSONArray
,我收到以下响应
[
[
"user_status": "1",
"pooja_name": "Festival Sevas",
"sess_date": "Mon Nov 30 2015",
"session_status": "Completed",
"message": "What ever message you want"
],
[
"user_status": "1",
"pooja_name": "Pushpalankara Seva",
"sess_date": "Tue Dec 15 2015",
"session_status": "Pending",
"message": "What ever message you want"
]
]
我能够解析这两个数组,但在我的两个 listview
中都显示 Pushpalankara Seva,我正在尝试在我的第一个列表视图中显示 Pushpalankara Seva 和第二个 Festival Sevas
class LoadPoojas extends AsyncTask<String, String, ArrayList<HashMap<String,String>>>
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute()
super.onPreExecute();
pDialog = new ProgressDialog(AboutUsFragment.this.getActivity());
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(true);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(false);
pDialog.show();
protected ArrayList<HashMap<String,String>> doInBackground(String... args)
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String,String>> upcomingdata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(POOJA_LISTING_URL, ServiceHandler.GET);
map = new HashMap<String, String>();
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null)
try
JSONArray jsonObj = new JSONArray(jsonStr);
// Getting JSON Array node
JSONArray pastarray = jsonObj.getJSONArray(0);
for (int i = 0; i < pastarray.length(); i++)
JSONObject c = pastarray.getJSONObject(i);
// creating new HashMap
// adding each child node to HashMap key => value
map.put(POOJA_LISTING_NAME, c.getString(POOJA_LISTING_NAME));
JSONArray upcoming = jsonObj.getJSONArray(1);
for (int i = 0; i < upcoming.length(); i++)
JSONObject c = upcoming.getJSONObject(i);
// creating new HashMap
// HashMap<String, String> upcomingmap = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(POOJA_LISTING_NAME, c.getString(POOJA_LISTING_NAME));
data.add(map);
catch (JSONException e)
e.printStackTrace();
else
Log.e("ServiceHandler", "Couldn't get any data from the url");
return data;
protected void onPostExecute(ArrayList<HashMap<String,String>> result)
super.onPostExecute(result);
/*if(interestaccept == null || interestaccept.length() == 0)
// Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_SHORT).show();
noacpt.setText(" No Accepted List ");
else
noacpt.setVisibility(View.INVISIBLE);
*/
// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
aList = new ArrayList<HashMap<String, String>>();
aList.addAll(result);
adapter = new CustomAdapterPooja(getActivity(),result);
completedpooja.setAdapter(adapter);
adapterupcoming = new CustomAdapterPoojaUpcoming(getActivity(),result);
notcompletedpooja.setAdapter(adapterupcoming);
adapter.notifyDataSetChanged();
adapterupcoming.notifyDataSetChanged();
completedpooja.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
);
/* upcomingaList = new ArrayList<HashMap<String, String>>();
upcomingaList.addAll(result);
adapterupcoming = new CustomAdapterPoojaUpcoming(getActivity(),result);
notcompletedpooja.setAdapter(adapterupcoming);
adapterupcoming.notifyDataSetChanged();
notcompletedpooja.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
);*/
【问题讨论】:
【参考方案1】:我检查了您的代码,发现您在两个数组(在两个 For 循环内)中的 hashmap with same key 中确实出错了。所以它会按照 hashmap 的规则用最新的值覆盖。
解决方案:
选项1:获取hashmap的arraylist并为每个新记录创建新的hashmap。
选项2:取POJO类的arraylist。
编辑:
public class UserPOJO
public String user_status;
public String pooja_name;
public String sess_date;
public String session_status;
public String message;
获取 POJO 类的数组列表,如下所示
public ArrayList<UserPOJO> userPOJOs = new ArrayList<UserPOJO>();
现在从 JSONArray 向数组列表中插入数据。
【讨论】:
需要创建anther arraylist?? 如果我需要没有 pojo 怎么办 在for循环中每次创建map对象,并在for循环的最后一点添加到hashmap的arraylist中。 data.add(map);在最后一行的 for 循环中。 让我们continue this discussion in chat。以上是关于如何使用 Listview 访问多个 JSONArray?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用Stringbuilder将多个Listview Item插入sqldatabase?
使用android中的自定义arrayadapter在listview中使用复选框检查后如何删除多个项目?