我目前正在完成一项课程,我需要使用android解析XML文件,并在Android手机上以有用的格式显示其数据。
我使用过XmlPullParser并成功将文件解析为listView。 结果如下:
为了改善上面的布局,我设置程序单独显示标题(即A78 Seamill等),当用户点击标题时,我希望有关作品的信息显示为下拉列表。
在我的MainActivity.java类中,我有一个如下所示的setOnItemClickListener:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long title) {
item.setExpand(true);
}
});
我的Item.java类中也有一个toString方法,如下所示:
@Override
public String toString() {
String s = "";
if (getExpand() == true) {
s = title + "\n" + description + "\n" + link + "\n" + georsspoint + "\n" + pubDate;
} else if (getExpand() == false) {
s = title;
}
return s;
}
我希望当我的onItemClickListener将布尔值“expand”设置为true时,我的toString方法会返回附加信息。
我知道第一条建议是使用expandableListView。我已经尝试过这个,不幸的是由于我检索数据的方式不起作用:
List<Item> items = null;
try {
XmlPullParserHandler parser = new XmlPullParserHandler();
items = parser.parse(getAssets().open("data.xml"));
ArrayAdapter<Item> adapter =
new ArrayAdapter<Item>(this,R.layout.list_item, items);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
我们非常感谢您提出任何建议或意见吗? 提前谢谢
此致 帕特里克
更新: 没有ArrayAdapter类,但我的完整XmlParserHandler类可能用于向您展示如何解析xml:
package com.example.traffic2;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Patrick Lafferty on 26/02/2018.
*/
public class XmlPullParserHandler {
List<Item> items;
private Item item;
private String text;
public XmlPullParserHandler() {
items = new ArrayList<Item>();
}
public List<Item> getItems() {
return items;
}
public List<Item> parse(InputStream is) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("item")) {
// create a new instance of item
item = new Item();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("item")) {
// add employee object to list
items.add(item);
}else if (tagname.equalsIgnoreCase("title")) {
item.setTitle(text);
} else if (tagname.equalsIgnoreCase("description")) {
item.setDescription(text);
} else if (tagname.equalsIgnoreCase("link")) {
item.setLink(text);
} else if (tagname.equalsIgnoreCase("pubData")) {
item.setPubDate(text);
} else if (tagname.equalsIgnoreCase("georsspoint")) {
item.setGeorsspoint(text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return items;
}
}
更新2 =请参阅下面的item.java类:
package com.example.traffic2;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Patrick Lafferty on 01/03/2018.
*/
public class Item {
private String title;
private String description;
private String link;
private String georsspoint;
private String pubDate;
private boolean expand = false;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getGeorsspoint() {
return georsspoint;
}
public void setGeorsspoint(String georsspoint) {
this.georsspoint = georsspoint;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public boolean getExpand() {
if (expand == false) {
return expand = false;
} else {
return expand = true;
}
}
public void setExpand(boolean expand) {
this.expand = expand;
}
@Override
public String toString() {
String s = "";
if (getExpand() == true) {
s = title + "\n" + description + "\n" + link + "\n" + georsspoint + "\n" + pubDate;
} else if (getExpand() == false) {
s = title;
}
return s;
}
}
答案 0 :(得分:1)
在你的Item.java中,你实现了toString()方法,在那个toString()中,你检查是否花费了文本。
您唯一缺少的是在调用setExpand()之后刷新列表适配器。要刷新列表适配器,您需要调用notifyDataSetChanged()。
final ArrayAdapter<Item> adapter =
new ArrayAdapter<Item>(this,R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long title) {
items.get(position).setExpand(true);
adapter.notifyDataSetChanged();
}
});
答案 1 :(得分:0)
您应该使用RecyclerView而不是ListView。然后,您将有ItemAnimator可用于处理插入动画。然后你所要做的就是调用notifyItemInserted来产生所需的效果。
教程: http://frogermcs.github.io/recyclerview-animations-androiddevsummit-write-up/