如何从资产文件中读取的xml数据动态创建可点击的文本视图?

时间:2013-04-03 10:52:41

标签: android xml eclipse textview clickable

我现在已经完成了我的作业。

我有一个应用程序,它读取一个资产xml文件,其中包含第一个字段以大写字母开头的条目。

我读了xml,我想动态地创建CLICKABLE textview,这样我就可以显示按首字母分组的其余数据。

我在这里添加了我的应用程序中的所有相关文件。

现在所有应用程序都显示一个Log.v,我确实阅读了我需要的所有数据。

我故意将layout.xml留空了。我想要动态创建文本视图。

任何人为我编写剩下的代码!

我希望你为我做的或者告诉我的是 - 当我读取XML文件时 - 我想如何为我在字段中找到的每个新字母动态创建一个可点击的textView并显示它在布局上......

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.barebonesxmlread"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.barebonesxmlread.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

layout(activity_main.xml):

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />

</RelativeLayout>

资产order.xml(我原本保留了全长558条记录!):

<germany_licence_plates>
<licence_plate><abbrev>A</abbrev><full_name>Augsburg</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>AA</abbrev><full_name>Aalen Ostalbkreis</full_name><land_name>Baden-Wuerttemberg</land_name></licence_plate>
<licence_plate><abbrev>AB</abbrev><full_name>Aschaffenburg</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>B</abbrev><full_name>Berlin</full_name><land_name>Berlin</land_name></licence_plate>
<licence_plate><abbrev>BA</abbrev><full_name>Bamberg</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>BAD</abbrev><full_name>Baden-Baden</full_name><land_name>Baden-Wuerttemberg</land_name></licence_plate>
<licence_plate><abbrev>BAR</abbrev><full_name>Barnim</full_name><land_name>Brandenburg</land_name></licence_plate>
<licence_plate><abbrev>BB</abbrev><full_name>Boeblingen</full_name><land_name>Baden-Wuerttemberg</land_name></licence_plate>
<licence_plate><abbrev>C</abbrev><full_name>Chemnitz</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>CA</abbrev><full_name>Calau</full_name><land_name>Brandenburg</land_name></licence_plate>
<licence_plate><abbrev>CB</abbrev><full_name>Cottbus</full_name><land_name>Brandenburg</land_name></licence_plate>
<licence_plate><abbrev>CE</abbrev><full_name>Celle</full_name><land_name>Niedersachsen</land_name></licence_plate>
<licence_plate><abbrev>DU</abbrev><full_name>Duisburg</full_name><land_name>Nordrhein-Westfalen</land_name></licence_plate>
<licence_plate><abbrev>DUW</abbrev><full_name>Bad Duerkheim / Neustadt/Weinstrasse</full_name><land_name>Rheinland-Pfalz</land_name></licence_plate>
<licence_plate><abbrev>DW</abbrev><full_name>Dippoldiswalde-Weisseritzkreis</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>DZ</abbrev><full_name>Delitzsch</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>ES</abbrev><full_name>Esslingen/Neckar</full_name><land_name>Baden-Wuerttemberg</land_name></licence_plate>
<licence_plate><abbrev>ESA</abbrev><full_name>Eisenach</full_name><land_name>Thueringen</land_name></licence_plate>
<licence_plate><abbrev>ESW</abbrev><full_name>Werra-Meissner-Kreis / Eschwege</full_name><land_name>Hess</land_name></licence_plate>
<licence_plate><abbrev>EU</abbrev><full_name>Euskirchen</full_name><land_name>Nordrhein-Westfalen</land_name></licence_plate>
<licence_plate><abbrev>EW</abbrev><full_name>Eberswalde</full_name><land_name>Brandenburg</land_name></licence_plate>
<licence_plate><abbrev>F</abbrev><full_name>Frankfurt/Main</full_name><land_name>Hess</land_name></licence_plate>
<licence_plate><abbrev>FB</abbrev><full_name>Wetteraukreis / Friedberg</full_name><land_name>Hess</land_name></licence_plate>
<licence_plate><abbrev>FD</abbrev><full_name>Fulda</full_name><land_name>Hess</land_name></licence_plate>
<licence_plate><abbrev>GU</abbrev><full_name>Guestrow</full_name><land_name>Mecklenburg-Vorpommern</land_name></licence_plate>
<licence_plate><abbrev>GUB</abbrev><full_name>Guben</full_name><land_name>Brandenburg</land_name></licence_plate>
<licence_plate><abbrev>GVM</abbrev><full_name>Grevesmuehlen</full_name><land_name>Mecklenburg-Vorpommern</land_name></licence_plate>
<licence_plate><abbrev>GW</abbrev><full_name>Greifswald/Landkreis</full_name><land_name>Mecklenburg-Vorpommern</land_name></licence_plate>
<licence_plate><abbrev>GZ</abbrev><full_name>Guenzburg</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>WT</abbrev><full_name>Waldshut-Tiengen</full_name><land_name>Baden-Wuerttemberg</land_name></licence_plate>
<licence_plate><abbrev>WTM</abbrev><full_name>Wittmund</full_name><land_name>Niedersachsen</land_name></licence_plate>
<licence_plate><abbrev>WU</abbrev><full_name>Wuerzburg</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>WUG</abbrev><full_name>Weissenburg-Gunzenhausen</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>WUN</abbrev><full_name>Wunsiedel</full_name><land_name>Bayern</land_name></licence_plate>
<licence_plate><abbrev>WUR</abbrev><full_name>Wurzen</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>WW</abbrev><full_name>Westerwald / Montabaur</full_name><land_name>Rheinland-Pfalz</land_name></licence_plate>
<licence_plate><abbrev>WZL</abbrev><full_name>Wanzleben</full_name><land_name>Sachsen-Anhalt</land_name></licence_plate>
<licence_plate><abbrev>X</abbrev><full_name>Bundeswehr fuer NATO-Hauptquartiere</full_name><land_name>Special</land_name></licence_plate>
<licence_plate><abbrev>Y</abbrev><full_name>Bundeswehr</full_name><land_name>Special</land_name></licence_plate>
<licence_plate><abbrev>Z</abbrev><full_name>Zwickauer Land</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>ZE</abbrev><full_name>Zerbst</full_name><land_name>Sachsen-Anhalt</land_name></licence_plate>
<licence_plate><abbrev>ZI</abbrev><full_name>Saechsischer Oberlausitzkreis Zittau</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>ZP</abbrev><full_name>Zschopau</full_name><land_name>Sachsen</land_name></licence_plate>
<licence_plate><abbrev>ZR</abbrev><full_name>Zeulenroda</full_name><land_name>Thueringen</land_name></licence_plate>
<licence_plate><abbrev>ZS</abbrev><full_name>Zossen</full_name><land_name>Brandenburg</land_name></licence_plate>
<licence_plate><abbrev>ZW</abbrev><full_name>Zweibruecken</full_name><land_name>Rheinland-Pfalz</land_name></licence_plate>
<licence_plate><abbrev>ZZ</abbrev><full_name>Zeitz</full_name><land_name>Sachsen-Anhalt</land_name></licence_plate>
</germany_licence_plates>

mainActivity.java:

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<GermanyPlatesInfo> 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
}

orderHandler.java:

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<GermanyPlatesInfo> plateList;

 public ArrayList<GermanyPlatesInfo> 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<GermanyPlatesInfo>();
  } 
  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);
  }

 }

}

GermanyPlatesInfo.java:

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;
 }


}

的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">bareBonesXMLRead</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Barebones XML Read!</string>

</resources>

我忘记了什么!请教!

1 个答案:

答案 0 :(得分:2)

您需要创建循环,具体取决于包含已解析数据的arraylist的大小。 在这里创建动态文本视图的代码是:

for (int i = 0; i < Tel.size(); i++) {

                final TextView phone = new TextView(context);
                phone.setTextAppearance(context,
                        android.R.style.TextAppearance_Medium);
                phone.setText(Tel.get(i));
                phone.setTypeface(Typeface.DEFAULT);
                phone.setTextColor(Color.BLUE);
                phone.setPaintFlags(phone.getPaintFlags()
                        | Paint.UNDERLINE_TEXT_FLAG);

                phone.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Call(phone.getText().toString());

                    }
                });

                InfoContainer.addView(phone);

            }

此处Tel是我的数组列表,其中包含所有电话号码。 InfoContainer是我的父线性布局,我在其中添加了这些动态创建的文本视图。