我已經完成了我的功課。如何從從資產文件中讀取的xml數據動態創建可單擊的文本視圖?
我有一個應用程序讀取資產的XML文件,其中包含第一個字段以大寫字母開頭的條目。
我讀了XML,我想DYNAMICALLY創建CLICKABLE textviews,以便我可以顯示按第一個字母分組的其餘數據。
我在這裏包括我的應用程序的所有相關文件。
現在所有的應用程序都會顯示Log.v,我會讀取所有需要的數據。
我故意讓layout.xml幾乎爲空。我想動態創建textviews。
任何一個爲我寫的代碼的其餘部分!
我希望你爲我做什麼或者告訴我是如何 - 當我閱讀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個記錄origi NAL):
<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>
我忘了什麼!問!
你不知道我是多麼感激我才能得到答案,而且你無法想象我剛剛接觸到這個問題。 我需要你,如果你打算用它,做以下事情: 告訴我,我應該在哪裏堅持代碼; 將您在該片段中使用的變量的名稱更改爲我在代碼中使用的變量的名稱,因爲我非常懷疑。這對我非常有幫助!謝謝... –