title: How do I dynamically create clickable textviews from xml data read from an asset file?
body: <p>I have done my homework now.</p>
  
  <p>I have an application that reads an asset xml file containing entries which first field begins with an upper case letter.</p>
  
  <p>I read the xml, and I would like to DYNAMICALLY create CLICKABLE textviews so that I can show the rest of the data grouped by first letter.</p>
  
  <p>I include here all the relevant files from my app.</p>
  
  <p>All the app does right now is show with a Log.v that I do read all the data I need.</p>
  
  <p>I have deliberately left the layout.xml pretty much empty. I DO want to create the textviews dynamically.</p>
  
  <p>Any one to write the rest of the code for me!</p>
  
  <p>What I would like you to do for me, or show me, is how--I suppose when I read the XML file--to dynamically create one clickable textView for each new alphabet letter I find in the  field and show it on the layout...</p>
  
  <p>Manifest file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.barebonesxmlread"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.barebonesxmlread.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>layout (activity_main.xml):</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" /&gt;
  
              &lt;LinearLayout
                  android:id="@+id/linearLayout1"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>The asset order.xml (I spared you the full-length 558 records original!):</p>
  
  <pre><code>&lt;germany_licence_plates&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;A&lt;/abbrev&gt;&lt;full_name&gt;Augsburg&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;AA&lt;/abbrev&gt;&lt;full_name&gt;Aalen Ostalbkreis&lt;/full_name&gt;&lt;land_name&gt;Baden-Wuerttemberg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;AB&lt;/abbrev&gt;&lt;full_name&gt;Aschaffenburg&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;B&lt;/abbrev&gt;&lt;full_name&gt;Berlin&lt;/full_name&gt;&lt;land_name&gt;Berlin&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;BA&lt;/abbrev&gt;&lt;full_name&gt;Bamberg&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;BAD&lt;/abbrev&gt;&lt;full_name&gt;Baden-Baden&lt;/full_name&gt;&lt;land_name&gt;Baden-Wuerttemberg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;BAR&lt;/abbrev&gt;&lt;full_name&gt;Barnim&lt;/full_name&gt;&lt;land_name&gt;Brandenburg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;BB&lt;/abbrev&gt;&lt;full_name&gt;Boeblingen&lt;/full_name&gt;&lt;land_name&gt;Baden-Wuerttemberg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;C&lt;/abbrev&gt;&lt;full_name&gt;Chemnitz&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;CA&lt;/abbrev&gt;&lt;full_name&gt;Calau&lt;/full_name&gt;&lt;land_name&gt;Brandenburg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;CB&lt;/abbrev&gt;&lt;full_name&gt;Cottbus&lt;/full_name&gt;&lt;land_name&gt;Brandenburg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;CE&lt;/abbrev&gt;&lt;full_name&gt;Celle&lt;/full_name&gt;&lt;land_name&gt;Niedersachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;DU&lt;/abbrev&gt;&lt;full_name&gt;Duisburg&lt;/full_name&gt;&lt;land_name&gt;Nordrhein-Westfalen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;DUW&lt;/abbrev&gt;&lt;full_name&gt;Bad Duerkheim / Neustadt/Weinstrasse&lt;/full_name&gt;&lt;land_name&gt;Rheinland-Pfalz&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;DW&lt;/abbrev&gt;&lt;full_name&gt;Dippoldiswalde-Weisseritzkreis&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;DZ&lt;/abbrev&gt;&lt;full_name&gt;Delitzsch&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ES&lt;/abbrev&gt;&lt;full_name&gt;Esslingen/Neckar&lt;/full_name&gt;&lt;land_name&gt;Baden-Wuerttemberg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ESA&lt;/abbrev&gt;&lt;full_name&gt;Eisenach&lt;/full_name&gt;&lt;land_name&gt;Thueringen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ESW&lt;/abbrev&gt;&lt;full_name&gt;Werra-Meissner-Kreis / Eschwege&lt;/full_name&gt;&lt;land_name&gt;Hess&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;EU&lt;/abbrev&gt;&lt;full_name&gt;Euskirchen&lt;/full_name&gt;&lt;land_name&gt;Nordrhein-Westfalen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;EW&lt;/abbrev&gt;&lt;full_name&gt;Eberswalde&lt;/full_name&gt;&lt;land_name&gt;Brandenburg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;F&lt;/abbrev&gt;&lt;full_name&gt;Frankfurt/Main&lt;/full_name&gt;&lt;land_name&gt;Hess&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;FB&lt;/abbrev&gt;&lt;full_name&gt;Wetteraukreis / Friedberg&lt;/full_name&gt;&lt;land_name&gt;Hess&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;FD&lt;/abbrev&gt;&lt;full_name&gt;Fulda&lt;/full_name&gt;&lt;land_name&gt;Hess&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;GU&lt;/abbrev&gt;&lt;full_name&gt;Guestrow&lt;/full_name&gt;&lt;land_name&gt;Mecklenburg-Vorpommern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;GUB&lt;/abbrev&gt;&lt;full_name&gt;Guben&lt;/full_name&gt;&lt;land_name&gt;Brandenburg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;GVM&lt;/abbrev&gt;&lt;full_name&gt;Grevesmuehlen&lt;/full_name&gt;&lt;land_name&gt;Mecklenburg-Vorpommern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;GW&lt;/abbrev&gt;&lt;full_name&gt;Greifswald/Landkreis&lt;/full_name&gt;&lt;land_name&gt;Mecklenburg-Vorpommern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;GZ&lt;/abbrev&gt;&lt;full_name&gt;Guenzburg&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WT&lt;/abbrev&gt;&lt;full_name&gt;Waldshut-Tiengen&lt;/full_name&gt;&lt;land_name&gt;Baden-Wuerttemberg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WTM&lt;/abbrev&gt;&lt;full_name&gt;Wittmund&lt;/full_name&gt;&lt;land_name&gt;Niedersachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WU&lt;/abbrev&gt;&lt;full_name&gt;Wuerzburg&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WUG&lt;/abbrev&gt;&lt;full_name&gt;Weissenburg-Gunzenhausen&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WUN&lt;/abbrev&gt;&lt;full_name&gt;Wunsiedel&lt;/full_name&gt;&lt;land_name&gt;Bayern&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WUR&lt;/abbrev&gt;&lt;full_name&gt;Wurzen&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WW&lt;/abbrev&gt;&lt;full_name&gt;Westerwald / Montabaur&lt;/full_name&gt;&lt;land_name&gt;Rheinland-Pfalz&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;WZL&lt;/abbrev&gt;&lt;full_name&gt;Wanzleben&lt;/full_name&gt;&lt;land_name&gt;Sachsen-Anhalt&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;X&lt;/abbrev&gt;&lt;full_name&gt;Bundeswehr fuer NATO-Hauptquartiere&lt;/full_name&gt;&lt;land_name&gt;Special&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;Y&lt;/abbrev&gt;&lt;full_name&gt;Bundeswehr&lt;/full_name&gt;&lt;land_name&gt;Special&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;Z&lt;/abbrev&gt;&lt;full_name&gt;Zwickauer Land&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZE&lt;/abbrev&gt;&lt;full_name&gt;Zerbst&lt;/full_name&gt;&lt;land_name&gt;Sachsen-Anhalt&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZI&lt;/abbrev&gt;&lt;full_name&gt;Saechsischer Oberlausitzkreis Zittau&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZP&lt;/abbrev&gt;&lt;full_name&gt;Zschopau&lt;/full_name&gt;&lt;land_name&gt;Sachsen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZR&lt;/abbrev&gt;&lt;full_name&gt;Zeulenroda&lt;/full_name&gt;&lt;land_name&gt;Thueringen&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZS&lt;/abbrev&gt;&lt;full_name&gt;Zossen&lt;/full_name&gt;&lt;land_name&gt;Brandenburg&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZW&lt;/abbrev&gt;&lt;full_name&gt;Zweibruecken&lt;/full_name&gt;&lt;land_name&gt;Rheinland-Pfalz&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;licence_plate&gt;&lt;abbrev&gt;ZZ&lt;/abbrev&gt;&lt;full_name&gt;Zeitz&lt;/full_name&gt;&lt;land_name&gt;Sachsen-Anhalt&lt;/land_name&gt;&lt;/licence_plate&gt;
  &lt;/germany_licence_plates&gt;
  </code></pre>
  
  <p>mainActivity.java:</p>
  
  <pre><code>package com.example.barebonesxmlread;
  
  import java.io.InputStream;
  import java.util.ArrayList;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  import org.xml.sax.InputSource;
  import org.xml.sax.XMLReader;
  
  import com.example.barebonesxmlread.GermanyPlatesInfo;
  import com.example.barebonesxmlread.OrderXMLHandler;
  import com.example.barebonesxmlread.R;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.content.res.AssetManager;
  import android.util.Log;
  import android.view.Menu;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          parseXML();
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      private void parseXML()
      {
          AssetManager assetManager = getBaseContext().getAssets();
          try {
                InputStream is = assetManager.open("order.xml");
                SAXParserFactory spf = SAXParserFactory.newInstance();
                 SAXParser sp = spf.newSAXParser();
                 XMLReader xr = sp.getXMLReader();
  
                 OrderXMLHandler myXMLHandler = new OrderXMLHandler();
                 xr.setContentHandler(myXMLHandler);
                 InputSource inStream = new InputSource(is);
                 xr.parse(inStream);
  
                 ArrayList&lt;GermanyPlatesInfo&gt; plateList = myXMLHandler.getCartList();
                 int i = 0;
                 for(GermanyPlatesInfo germanyPlatesInfo: plateList)
                 {
                     Log.v("Data", ++i + ": " + germanyPlatesInfo.seqNo + ": "  + germanyPlatesInfo.getItemNumber() + " - " + germanyPlatesInfo.getLandName());
                 } // end of do all plates
                  is.close();     
              } // end of try
              catch (Exception e)
              {
                  Log.v("JPB catch in parseXML", e.getMessage(), e);
              } // end of catch
      } // end of parseXML function
  }
  </code></pre>
  
  <p>orderHandler.java:</p>
  
  <pre><code>package com.example.barebonesxmlread;
  
  import java.util.ArrayList;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  public class OrderXMLHandler extends DefaultHandler {
  
   boolean currentElement = false;
   String currentValue = "";
  
   GermanyPlatesInfo germanyPlatesInfo;
   ArrayList&lt;GermanyPlatesInfo&gt; plateList;
  
   public ArrayList&lt;GermanyPlatesInfo&gt; getCartList() {
    return plateList;
   }
  
   public void startElement(String uri, String localName, String qName,
     Attributes attributes) throws SAXException {
  
    currentElement = true;
  
    if (qName.equals("germany_licence_plates")){
     plateList = new ArrayList&lt;GermanyPlatesInfo&gt;();
    } 
    else if (qName.equals("licence_plate")) {
     germanyPlatesInfo = new GermanyPlatesInfo();
    }
  
   }
  
   public void endElement(String uri, String localName, String qName)
   throws SAXException {
  
    currentElement = false;
  
    if (qName.equalsIgnoreCase("abbrev"))
     germanyPlatesInfo.setSeqNo(currentValue.trim());
    else if (qName.equalsIgnoreCase("full_name"))
     germanyPlatesInfo.setItemNumber(currentValue.trim());
    else if (qName.equalsIgnoreCase("land_name"))
     germanyPlatesInfo.setLandName(currentValue.trim());
    else if (qName.equalsIgnoreCase("Price"))
     germanyPlatesInfo.setPrice(currentValue.trim());
    else if (qName.equalsIgnoreCase("licence_plate"))
     plateList.add(germanyPlatesInfo);
  
    currentValue = "";
   }
  
   public void characters(char[] ch, int start, int length)
   throws SAXException {
  
    if (currentElement) {
     currentValue = currentValue + new String(ch, start, length);
    }
  
   }
  
  }
  </code></pre>
  
  <p>GermanyPlatesInfo.java:</p>
  
  <pre><code>package com.example.barebonesxmlread;
  
  public class GermanyPlatesInfo {
  
   String seqNo = null;
   String itemNumber = "";
   String land_name = "";
   String price = "";
  
   public String getSeqNo() {
    return seqNo;
   }
   public void setSeqNo(String seqNo) {
    this.seqNo = seqNo;
   }
   public String getItemNumber() {
    return itemNumber;
   }
   public void setItemNumber(String itemNumber) {
    this.itemNumber = itemNumber;
   }
   public String getLandName() {
    return land_name;
   }
   public void setLandName(String land_name) {
    this.land_name = land_name;
   }
   public String getPrice() {
    return price;
   }
   public void setPrice(String price) {
    this.price = price;
   }
  
  
  }
  </code></pre>
  
  <p>strings.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  
      &lt;string name="app_name"&gt;bareBonesXMLRead&lt;/string&gt;
      &lt;string name="action_settings"&gt;Settings&lt;/string&gt;
      &lt;string name="hello_world"&gt;Barebones XML Read!&lt;/string&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>Did I forget anything! Ask!</p>