Java 无法在辅助屏幕上显示已解析的数据
我正在使用一个在线示例来了解如何解析应用程序中的XML文件。Java 无法在辅助屏幕上显示已解析的数据,java,android,xml,Java,Android,Xml,我正在使用一个在线示例来了解如何解析应用程序中的XML文件。 我使用了相同的employees.xml文件并复制了原样的代码,不同的是我不使用应用程序的主屏幕,而是使用一个菜单按钮打开的辅助屏幕。 我用当前使用的活动(PlacesActivity)替换了主活动的etc,但屏幕上没有显示任何内容 代码文件如下所示 我做错什么了吗?你能帮忙吗 PlacesActivity.java public class PlacesActivity extends AppCompatActivity { Li
我使用了相同的employees.xml文件并复制了原样的代码,不同的是我不使用应用程序的主屏幕,而是使用一个菜单按钮打开的辅助屏幕。
我用当前使用的活动(PlacesActivity)替换了主活动的etc,但屏幕上没有显示任何内容 代码文件如下所示 我做错什么了吗?你能帮忙吗 PlacesActivity.java
public class PlacesActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
listView = (ListView) findViewById(R.id.list);
List<Employee> employees = null;
try{
XMLPullParserHandler parser = new XMLPullParserHandler();
employees = parser.parse(getAssets().open("employees.xml"));
ArrayAdapter<Employee> adapter =
new ArrayAdapter<Employee>(this, R.layout.list_item, employees);
listView.setAdapter(adapter);
}catch(IOException e) {
e.printStackTrace();
} }}
XMLPullParserHandler.java
public class XMLPullParserHandler {
List<Employee> employees;
private Employee employee;
private String text;
public XMLPullParserHandler(){
employees = new ArrayList<Employee>();
}
public List<Employee> getEmployees() {
return employees;
}
public List<Employee> 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("employee")){
employee = new Employee();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if(tagname.equalsIgnoreCase("employee")){
employees.add(employee);
}else if(tagname.equalsIgnoreCase("name")){
employee.setName(text);
}else if(tagname.equalsIgnoreCase("id")){
employee.setId(Integer.parseInt(text));
}else if(tagname.equalsIgnoreCase("department")){
employee.setDepartment(text);
}else if(tagname.equalsIgnoreCase("email")){
employee.setEmail(text);
}else if(tagname.equalsIgnoreCase("type")){
employee.setType(text);
}
break;
default:
break;
}
eventType = parser.next();
}
}catch (Exception e){
e.printStackTrace();
}
return employees;
}}
公共类XMLPullParserHandler{
列出员工名单;
私人雇员;
私有字符串文本;
公共XMLPullParserHandler(){
employees=newarraylist();
}
公开名单{
返回员工;
}
公共列表解析(InputStream为){
XmlPullParserFactory工厂=null;
XmlPullParser=null;
试一试{
factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser=factory.newPullParser();
setInput(is,null);
int eventType=parser.getEventType();
while(eventType!=XmlPullParser.END_文档){
字符串标记名=parser.getName();
开关(事件类型){
case XmlPullParser.START_标记:
if(标记名.equalsIgnoreCase(“员工”)){
雇员=新雇员();
}
打破
case XmlPullParser.TEXT:
text=parser.getText();
打破
case XmlPullParser.END_标记:
if(标记名.equalsIgnoreCase(“员工”)){
employees.add(employees);
}else if(标记名.equalsIgnoreCase(“名称”)){
employee.setName(文本);
}else if(标记名.equalsIgnoreCase(“id”)){
setId(Integer.parseInt(text));
}else if(标记名.equalsIgnoreCase(“部门”)){
员工。设置部门(文本);
}else if(标记名.equalsIgnoreCase(“电子邮件”)){
employee.setEmail(文本);
}else if(标记名.equalsIgnoreCase(“类型”)){
employee.setType(文本);
}
打破
违约:
打破
}
eventType=parser.next();
}
}捕获(例外e){
e、 printStackTrace();
}
返回员工;
}}
activity_places.xml
<LinearLayout 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:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employees:" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView></LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.media1.thesistest2.PlacesActivity"
tools:showIn="@layout/activity_places">
</RelativeLayout>
content_places.xml
<LinearLayout 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:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employees:" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView></LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.media1.thesistest2.PlacesActivity"
tools:showIn="@layout/activity_places">
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="16dp">
</TextView>
employees.xml
<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee>
<id>2165</id>
<name>Mike</name>
<department>Dev</department>
<type>Perm</type>
<email>mike@a.com</email>
</employee>
<employee>
<id>3463</id>
<name>Trav</name>
<department>DB</department>
<type>Contr</type>
<email>travis@a.com</email>
</employee>
<employee>
<id>2001</id>
<name>Mary</name>
<department>Test</department>
<type>Perm</type>
<email>mary@a.com</email>
</employee>
</employees>
2165
迈克
发展
烫发
mike@a.com
3463
列车
分贝
控制
travis@a.com
2001
玛丽
试验
烫发
mary@a.com
基本上,适配器的文本视图中没有设置任何数据
你应该能够用这个来验证这一点
try {
XMLPullParserHandler parser = new XMLPullParserHandler();
employees = parser.parse(getAssets().open("employees.xml"));
Log.d("num employees", "" + employees.size()); // Check the Logcat!
ArrayAdapter<Employee> adapter = ...
3) 使用您自己的ID值,但在ArrayAdapter
ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>(
this,
R.layout.list_item,
R.id.item_title, // for example
employees);
ArrayAdapter适配器=新的ArrayAdapter(
这
R.layout.list_项目,
R.id.item\u title,//例如
员工);
4) 定义您自己的
EmployeeAdapter
类,该类扩展了ArrayAdapter
我认为这是因为您没有在employees.xml中定义名称空间,但您有factory.setNamespaceAware(true)代码>
ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>(
this,
R.layout.list_item,
R.id.item_title, // for example
employees);
尝试以下操作:factory.setNamespaceAware(false)代码>我把employees.xml文件放错地方了。资产目录必须在项目的src/main下创建,我应该在那里放置我想要解析的xml文件。谢谢大家:)我使用了第一个选项,但仍然没有显示任何内容。。可能是其他原因出错了吗?您是否通过Logcat验证了列表中是否有数据?您有android:layout\u weight
但没有使用layout\u weightSum
我尝试添加一个weightSum,但仍然没有显示任何内容。这是互联网上的一个例子,所以我得到了它的真实情况。我想我在另一个屏幕而不是主屏幕上显示它的过程中可能做错了什么。因为它似乎适用于示例的创建者。您的员工列表是否已填写?您是指employees.xml文件?是的,它有3名员工。不,我指的是列表