从JSON android向Arrayrecieved添加新值

时间:2014-04-25 05:42:53

标签: java android json arraylist hashmap

我有一个JSON响应,我将其解析为一个数组。此响应包含多个具有类似NewsSourceId

的条目

我想用类似的NewsSourceId来表示条目,并为它们分配颜色。

我有数组中的颜色:

String[] colorPallete =new String[] {"#1F1A17", "#62934D", "#F9B03F", "#7959BC", "#74B8DE", "#E65641", "#7CC8BB", "#D7CE5D", "#D6BE95", "#B694D1"};

JSON解析: -

private class GetList extends AsyncTask<String, String, ArrayList<HashMap<String, String>>>{
protected ArrayList<HashMap<String, String>> doInBackground(String... params)
    {
        // TODO Auto-generated method stub

        JSONArray jObject;
        try 
        {
            jObject = new JSONArray(params[0]);
            for (int i = 0; i < jObject.length(); i++)
            {
                 JSONObject menuObject = jObject.getJSONObject(i);

                 String title= menuObject.getString("Title");
                 String description= menuObject.getString("BodyText");
                 String thumbnail= menuObject.getString("ThumbnailPath");
                 String newsUrl = menuObject.getString("Url");
                 String body = menuObject.getString("Body");
                 String newsBigImage = menuObject.getString("ImageBlobUrls");
                 String newsComments = menuObject.getString("NumberOfComments");
                 String newsViews = menuObject.getString("NumberOfViews");
                 String publishedDate = menuObject.getString("PublishedDate");
                 String articleGuid = menuObject.getString("ArticleGuid");
                 String newsSourceId = menuObject.getString("NewsSourceId");
                 String newsId  = menuObject.getString("ID");
                 String publisherName = menuObject.getString("AuthorDisplayName");
                 String newsSourceTitle = menuObject.getString("NewsSourceTitle");



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




                    map.put(NEWSSOURCETITLE, title);
                    map.put(TITLE, description);
                    map.put(THUMBNAILPATH, thumbnail);
                    map.put(BODY, body);
                    map.put(NEWSSOURCEID, newsSourceId);
                    map.put(PUBLISHER,publisherName);

                    myNewsList.add(map);




                }
}

此处myNewsList是一个ArrayList&gt;。我想将myNewsList传递给ListAdapter以显示在列表视图中。

 ArrayList<HashMap<String, String>> getList = new GetList().execute(items).get();
   itemsAdapter = new LazyAdapter(myContext, getList);
   newsList.setAdapter(itemsAdapter);
    newsList.setOnItemClickListener(new OnItemClickListener() 
            {

                @Override
                public void onItemClick(AdapterView<?> arg0,
                        View arg1, int position, long arg3) 
                {
                    // TODO Auto-generated method stub
                    myDialog = new ProgressDialog(myContext).show(getActivity(), "Fetching news..", "Just a moment");



                        HashMap<String, String> myMap = myNewsList.get(position);
                        Intent newsIntent = new Intent(getActivity(),NewsDetails.class);
                        newsIntent.putExtra("NewsItems", myMap);

                        startActivity(newsIntent);




                }
            });

另外,我需要在另一个活动中获取这些值,如下所示: -

data =  (HashMap<String, String>) in.getSerializableExtra("NewsItems");
             newUrl = data.get(HomeFragment.URL);
             newsTitle =data.get(HomeFragment.NEWSSOURCETITLE);
             newsBody=data.get(HomeFragment.BODY);
             newsImage = data.get(HomeFragment.IMAGEBLOBURLS);
             newsViews = data.get(HomeFragment.NUMBEROFVIEWS);
             newsComments = data.get(HomeFragment.NUMBEROFCOMMENTS);
             pubDate = data.get(Home.PUBLISHEDDATE);
             articleGuid = data.get(Home.ARTICLEGUID);
             newsSourceId = data.get(HomeFragment.NEWSSOURCEID);
             newsId = data.get(HomeFragment.ID);
             publisherName = data.get(HomeFragment.PUBLISHER);
             colorCode = data.get(HomeFragment.COLORCODE);

此外,当用户点击列表视图项时,下一个屏幕应该具有与NewsSourceId

相关联的颜色
dateView.setBackgroundColor(Color.parseColor(colorCode));

我该怎么做?

0 个答案:

没有答案