Android开发日记(五)

从服务器端传递多个数据

先在服务器端设置cs文件

 1 using Newtonsoft.Json;
 2 using Newtonsoft.Json.Linq;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Web;
 7 using System.Web.Mvc;
 8 
 9 namespace WebJSON.Controllers
10 {
11     public class adShowTitleController : Controller
12     {
13         DataInfo storedb = new DataInfo();
14         //
15         // GET: /JobDetails/
16 
17         public JArray Index(int adid = int.MaxValue)
18         {
19 
20             IEnumerable<ad> ads;
21             ads = storedb.ad.Where(u => u.adid < adid).OrderByDescending(u => u.adid).Take(15);
22             string json = JsonConvert.SerializeObject(ads);
23             return JArray.Parse(json);
24         }
25 
26     }
27 }
View Code

传递所有id小于所设置的最大值的json数组对象

然后在java客户端调用service方法取回这些json对象

在调用listItem方法用一个for循环设置所有listItem每层循环后加listItem.add(map);再生成一个新的listItem

listItem定义:ArrayList<HashMap<String, Object>> listItem;
                   SimpleAdapter listItemAdapter;

 1 listItem = new ArrayList<HashMap<String, Object>>();
 2     for (int i = 0; i < addetailsJsonArray.length(); i++) {
 3         JSONObject lectureJsonObject = addetailsJsonArray.optJSONObject(i);
 4         HashMap<String, Object> map = new HashMap<String, Object>();
 5         map.put("lectureid", lectureJsonObject.optInt("adid"));
 6         map.put("lecturetitle",lectureJsonObject.optString("adtitle").trim());
 7         map.put("reporter", lectureJsonObject.optString("adman").trim());
 8         map.put("lecturetime",lectureJsonObject.optString("addate").replace("T"," ").trim());
 9                         //.replace("T", " ").substring(0, 19));
10         //map.put("lecturesite", lectureJsonObject.optString("lecturesite").trim());
11         //map.put("campus",lectureJsonObject.optString("campus").trim());
12         //map.put("remark",lectureJsonObject.optString("remark").trim());
13         //map.put("depart", lectureJsonObject.optString("depart").trim());
14         //map.put("starttime", lectureJsonObject.optString("starttime").trim());
15         //lastLectureid=lectureJsonObject.optInt("id");
16         listItem.add(map);
17         // TODO Auto-generated method stub
18         listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
19                 R.layout.leturetitle_listview,// ListItem的XML实现
20                         // 动态数组与ImageItem对应的子项
21                 new String[] { "lectureid", "lecturetitle",
22                         "reporter", "lecturetime"},
23                 // ImageItem的XML文件里面的一个ImageView,两个TextView ID
24                 new int[] { R.id.textView_lectureid,
25                         R.id.textView_lecturetitle, R.id.textView_report,
26                         R.id.textView_lecturetime });
27         listViewNewsMoreTitles.setAdapter(listItemAdapter);
View Code

 

posted @ 2015-12-06 18:05  Wei_java  阅读(343)  评论(0编辑  收藏  举报