0

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

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

我阅读了 xml,我想动态创建 CLICKABLE 文本视图,以便我可以显示按首字母分组的其余数据。

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

该应用程序现在所做的只是使用 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>

布局(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);
  }

 }

}

德国PlatesInfo.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;
 }


}

字符串.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>

我是不是忘了什么!问!

4

1 回答 1

2

您需要根据包含已解析数据的数组列表的大小创建循环。在这里创建动态文本视图是代码:

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 是我的父线性布局,我在其中添加这些动态创建的文本视图。

于 2013-04-03T11:02:11.007 回答