gpt4 book ai didi

java - android:tableview中的xml解析输出

转载 作者:行者123 更新时间:2023-12-01 15:55:22 25 4
gpt4 key购买 nike

我是安卓新手。我已经搜索了代码并完成了 xml 解析,但它在 ListView 中......我需要它应该在表格 View 中请帮我...我的代码如下:

class MyXMLHandler

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

Boolean currentElement = false;
String currentValue = null;
public static SitesList sitesList = null;

public static SitesList getSitesList() {
return sitesList;
}

public static void setSitesList(SitesList sitesList) {
MyXMLHandler.sitesList = sitesList;
}

/** Called when tag starts ( ex:- <name>AndroidPeople</name>
* -- <name> )*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

currentElement = true;

if (localName.equals("maintag"))
{
/** Start */
sitesList = new SitesList();
} else if (localName.equals("website")) {
/** Get attribute value */
// String attr = attributes.getValue("category");
// sitesList.setCategory(attr);
}

}

/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {

currentElement = false;

/** set value */
if (localName.equalsIgnoreCase("name"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("website"))
sitesList.setWebsite(currentValue);

}

/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}

}

}

二级站点列表

import java.util.ArrayList;

/** Contains getter and setter method for variables */
public class SitesList {

/** Variables */
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> website = new ArrayList<String>();
// private ArrayList<String> category = new ArrayList<String>();

/** In Setter method default it will return arraylist
* change that to add */

public ArrayList<String> getName() {
return name;
}

public void setName(String name) {
this.name.add(name);
}

public ArrayList<String> getWebsite() {
return website;
}

public void setWebsite(String website) {
this.website.add(website);
}

/**public ArrayList<String> getCategory() {
return category;
}

public void setCategory(String category) {
this.category.add(category);
}**/

}

第三类是 MyParsingExample

import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class XMLParsingExample extends Activity {

/** Create Object For SiteList Class */
SitesList sitesList = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);

/** Create a new textview array to display the results */
TextView name[];
TextView website[];
//TextView category[];

try {

/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

/** Send URL to parse XML Tags */
URL sourceUrl = new URL("http://ipaddress/test/file.xml");

/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));

} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}

/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;

/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
//category = new TextView[sitesList.getName().size()];

/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
/**category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));**/

layout.addView(name[i]);
layout.addView(website[i]);
//layout.addView(category[i]);
}

/** Set the layout view to display */
setContentView(layout);

}

xml 文件是

<maintag>
<!-- Table item -->
<item>
<name>sam</name>
<website>sam</website>
</item>

最佳答案

好的 Chaaanleeenge 已接受

我想我能猜到你想做什么。首先,您的代码基本上执行以下操作:

  • 解析您的 XML 文件并将结果放入 SitesList
  • 以编程方式创建所有布局内容
  • 对于已解析的每个 Item,它都会生成一个新的 TextView

所以那里没有ListView。

您现在想要将解析后的值放入 TableView 中。所以我想说的是,构建生成布局的代码的方式并不是让演示内容远离代码的最佳方法。 Android 正是为此目的而精心设计的。您应该仔细查看http://developer.android.com/guide/topics/resources/index.html特别是布局资源。您可以在 xml 文件中进行布局。

回到你当前的问题。要在表格中显示您的内容,您必须编辑 MyParsingExample Activity 。

(我目前无法测试代码,但它应该按如下方式工作:)

/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);

此时您应该使用像这样的TableLayout:

/** Create a new layout to display the view */
TableLayout layout = new TableLayout(this);

所以 tableLayout 只是布局其他组件的另一种方式。

现在您需要看一下这部分:

/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
//category = new TextView[sitesList.getName().size()];

/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
/**category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));**/

layout.addView(name[i]);
layout.addView(website[i]);
//layout.addView(category[i]);
}

这就是生成 Textview 的点。您需要用一些在 TableView 中生成行的代码来替换它。正如我所说,我目前无法测试代码。但我指的是:

http://developer.android.com/reference/android/widget/TableLayout.htmlhttp://developer.android.com/reference/android/widget/TableRow.html

例如这样:

/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
//category = new TextView[sitesList.getName().size()];

/** Add rows for every entry and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
//we can leave that part as it is
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));

website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));

//now we create a new row
TableRow row = new TableRow(this);

//we add the textviews to the row
row.addView(name[i]);
row.addView(website[i]);

//and we add the row to the tablelayout
layout.addView(row);
}

我真的希望这能帮助你继续下去。我一般建议看看 http://developer.android.com/guide/topics/resources/available-resources.html这是开始学习 Android 编程的最佳页面。

关于java - android:tableview中的xml解析输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5180360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com