Tree of ListViews

   

Tap each line. A > means you can go deeper into the tree. The absence of a > means that the node is a leaf. If you tap a leaf, you go to the corresponding article in Wikipedia, formatted for a mobile device.

Source code in Tree.zip

  1. TreeActivity.java: a ListActivity containing a ListView.
  2. WikiActivity.java: launched by the other Activity by means of an Intent.
  3. main.xml: never used.
  4. AndroidManifest.xml: uses permission "android.permission.INTERNET" for the WebView in class WikiActivity. Two activity elements, one with the theme "@android:style/Theme.NoTitleBar".

The data structure

The underlying data structure is a tree, recursively containing smaller trees. When we go deeper into the tree, we push the Stack<Tree>. When we come back up, we pop the stack. The app is smart enough not to fall off the top or bottom of the tree. A leaf node is never pushed onto the stack. Instead, we start a simple Activity that displays a Wikipedia article.

There was so much data that I should have put it in a separate object, called a model (as in Model-View-Controller).

At any given time, the ListView is displaying a non-empty list of items. (An empty list [i.e., a leaf] would be displayed as a Wikipedia article.) Each non-empty list comes from a MyAdapter. The two most important methods of the MyAdapter are getCount and getView.

The Views in the ListView

Each view is a horizontal LinearLayout containing two TextViews, a left-justified name and a (possibly invisible) right-justified >. To give the the views the standard appearance, they are inflated from the TextView in android-sdks/platforms/android-14/data/res/layout/simple_list_item_1.xml.

Things to try

  1. Why don’t the Show buttons in Wikipedia work when we get to them via this app?

  2. Create the tree as a recursive resource in a .xml file. See TypedArray.