private void initFavoritePosts() { fFeed = getSharedPreferences("DATA", 0); String storeJSON = fFeed.getString("postData", ""); favoritePosts = new HNFeed(new ArrayList<HNPost>(), null, ""); if (storeJSON.length() > 0) { try { postJsonArray = new JSONArray(storeJSON); for (int n = 0; n < postJsonArray.length(); n++) { JSONObject temp = postJsonArray.getJSONObject(n); favoritePosts.addPost( new HNPost( temp.getString("url"), temp.getString("title"), temp.getString("urlDomain"), temp.getString("author"), temp.getString("postID"), temp.getInt("commentsCount"), temp.getInt("points"), null)); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public boolean addFavoritePost(int nPost) { fFeed = getSharedPreferences("DATA", 0); if (postJsonArray == null) { postJsonArray = new JSONArray(); } HNPost temp = mFeed.getPosts().get(nPost); if (!isPostinFavorite(temp.getURL())) { // New hnPOST JSONObject jsonObject = new JSONObject(); try { jsonObject.put("url", temp.getURL()); jsonObject.put("title", temp.getTitle()); jsonObject.put("urlDomain", temp.getURLDomain()); jsonObject.put("author", temp.getAuthor()); jsonObject.put("postID", temp.getPostID()); jsonObject.put("commentsCount", temp.getCommentsCount()); jsonObject.put("points", temp.getPoints()); jsonObject.put("upvoteURL", null); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // put in jsonArray postJsonArray.put(jsonObject); fFeed.edit().putString("postData", postJsonArray.toString()).commit(); favoritePosts.addPost(temp); return true; } return false; }