/** Topic hierarchy parsing */ public static TopicNode parseTopics(String json) throws JSONException { JSONObject jTopics = new JSONObject(json); ArrayList<TopicNode> topics = parseTopicChildren(jTopics); TopicNode root = new TopicNode(); root.setChildren(topics); return root; }
/** Reads through the given JSONObject and adds any topics to the given topic. */ private static ArrayList<TopicNode> parseTopicChildren(JSONObject jTopic) throws JSONException { // Don't allocate any lists if it's empty. if (jTopic.length() == 0) { return null; } @SuppressWarnings("unchecked") Iterator<String> iterator = jTopic.keys(); String topicName; ArrayList<TopicNode> topics = new ArrayList<TopicNode>(); TopicNode currentTopic; while (iterator.hasNext()) { topicName = iterator.next(); currentTopic = new TopicNode(topicName); currentTopic.setChildren(parseTopicChildren(jTopic.getJSONObject(topicName))); topics.add(currentTopic); } return topics; }
@Override public void onCreate(Bundle savedState) { super.onCreate(savedState); setContentView(R.layout.topic_view); showLoading(R.id.loading_container); if (savedState == null) { mTopicView = new TopicViewFragment(); Bundle extras = getIntent().getExtras(); if (extras != null) { if (extras.containsKey(GuideViewActivity.TOPIC_NAME_KEY)) { setTitle(extras.getString(GuideViewActivity.TOPIC_NAME_KEY)); } mTopicView.setArguments(extras); } FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.topic_view_fragment, mTopicView); ft.commit(); } else { mTopicView = (TopicViewFragment) getSupportFragmentManager().findFragmentById(R.id.topic_view_fragment); } mTopicNode = (TopicNode) getIntent().getSerializableExtra(TOPIC_KEY); if (mTopicNode != null) { setTitle(mTopicNode.getDisplayName()); App.sendScreenView("/category/" + mTopicNode.getName()); } }