如何将 json 打印到 listview 包含两个数据?

我想通过 json 输入在 listview 上显示两个数据。所有代码都来自互联网,我得到了错误。我没有清晰的逻辑来理解代码的含义。请给我建议并更改我的代码。


  private void displayArrayList(String jsonStr){


        String[] from = {"eventName", "date"};

        int[] to = {R.id.eventName, R.id.date};


        SimpleAdapter  simpleAdapter = new SimpleAdapter (

                getActivity(),convertToWordArrayList(jsonStr), R.layout.listview_layout,from,to);

        simpleAdapter.notifyDataSetChanged();

        listView.setAdapter(simpleAdapter);

    }


    private ArrayList<HashMap<String, ActivityInfo>> convertToWordArrayList(String jsonStr){

        JSONObject jsonObject ;


        ArrayList<HashMap<String, ActivityInfo>> arrayList = new ArrayList<HashMap<String, ActivityInfo>>();


        try{

            jsonObject = new JSONObject(jsonStr);

            JSONArray jsonArray=jsonObject.getJSONArray("article");


            for (int i=0;i<jsonArray.length();i++){

                JSONObject jsonObjRow=jsonArray.getJSONObject(i);

                ActivityInfo activityInfo =new ActivityInfo();


                activityInfo.eventName = jsonObjRow.getString("eventName");

                activityInfo.date = jsonObjRow.getString("date");


                HashMap<String, String> map= new HashMap<String, ActivityInfo>();

                map.put("eventName", activityInfo.eventName );

                map.put("date", activityInfo.date);


                JSONArray jsonArray2=jsonObjRow.getJSONArray("content");

                for (int j=0;j<jsonArray2.length();j++) {

                    JSONObject jsonObjRow2 = jsonArray2.getJSONObject(j);

                    activityInfo.review = jsonObjRow2.getString("review");

                }

                arrayList.add(activityInfo);

            }

        }catch (JSONException e){

            e.printStackTrace();

        }


        return arrayList;

    }


开满天机
浏览 111回答 2
2回答

胡子哥哥

这是你的模型类..ActivityInfo.javapublic class ActivityInfo implements Serializable {String eventName;String date;public void setEventName(String eventName){&nbsp; &nbsp; this.eventName = eventName ;}public String getEventName(){&nbsp; &nbsp; return this.eventName;}public void setDate(String date){&nbsp; &nbsp; this.date = date;}public String getDate(){&nbsp; &nbsp; return date;}}这将是您最终的无错误代码..private void displayArrayList(String jsonStr){&nbsp; &nbsp; String[] from = {"eventName", "date"};&nbsp; &nbsp; int[] to = {R.id.eventName, R.id.date};&nbsp; &nbsp; SimpleAdapter&nbsp; simpleAdapter = new SimpleAdapter (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getActivity(),convertToWordArrayList(jsonStr), R.layout.listview_layout,from,to);&nbsp; &nbsp; simpleAdapter.notifyDataSetChanged();&nbsp; &nbsp; listView.setAdapter(simpleAdapter);}private ArrayList<HashMap<String,String>> convertToWordArrayList(String jsonStr){&nbsp; &nbsp; JSONObject jsonObject;&nbsp; &nbsp; ArrayList<HashMap<String,String>> arrayList = new ArrayList();&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; jsonObject = new JSONObject(jsonStr);&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray=jsonObject.getJSONArray("article");&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0;i<jsonArray.length();i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObjRow=jsonArray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<String,String> hashMap=new HashMap<>();//create a hashmap to store the data in key value pair&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashMap.put("eventName",jsonObjRow.getString("eventName"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashMap.put("date",jsonObjRow.getString("date"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray2=jsonObjRow.getJSONArray("content");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*If you want to get Content Reviews from Json, you need to make another attributes like Content in ActivityInfo Class*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrayList.add(hashmap);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }catch (JSONException e){&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return arrayList;&nbsp;}

HUX布斯

我完全这样做:public class menuCreation(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string Category { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string ItemName { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string Price { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string FileName { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public menuCreation[] Arr { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stream responseStream = response.GetResponseStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StreamReader reader = new StreamReader(responseStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string json = (reader.ReadToEnd());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<menuCreation> items = JsonConvert.DeserializeObject<List<menuCreation>>(json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arr = items.ToArray();}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; menuCreation mc = new menuCreation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in mc.Arr)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PictureBox pb = new PictureBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pb.Tag = item.ItemName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pb.Name = item.Price;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}您可以看到,我使用类对象到达 itemName。因此,您可以使用此方法添加列表视图。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java