无法在辅助屏幕上显示已解析的数据

时间:2017-03-07 10:44:55

标签: java android xml

我正在使用此video在线找到的示例来了解如何在应用中解析XML文件。
我使用了相同的employees.xml文件并按原样复制了代码,不同之处在于我没有使用应用程序的主屏幕,而是使用菜单按钮打开辅助文件。
我用当前使用的Activity(PlacesActivity)取代了MainActivity等,但我没有在屏幕上显示任何内容。

代码文件如下所示。

我做错了吗?能否请你帮忙?

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

Employee.java

public class Employee {

private String name;
private int id;
private String department;
private String type;
private String email;

public String getName() {
    return name;
}

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

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getDepartment() {
    return department;
}

public void setDepartment(String department) {
    this.department = department;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Override
public String toString() {

    return id + ": " + name + "\n" + department + "-" + type + "\n" + email;
}}

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;
}}

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>

content_places.xml

<?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>

3 个答案:

答案 0 :(得分:0)

基本上,没有数据被设置到适配器的TextView中。

您应该可以使用此

验证
try {
    XMLPullParserHandler parser = new XMLPullParserHandler();
    employees = parser.parse(getAssets().open("employees.xml"));

    Log.d("num employees", "" + employees.size()); // Check the Logcat!

    ArrayAdapter<Employee> adapter = ...

您有几个选择

1)使用默认项目布局

ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>(
        this, 
        android.R.layout.simple_list_item_1, 
        employees);

2)使用您的布局,但在您的android:id/text1

中添加item_list.xml ID
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"       <!--- See here --->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:textSize="16dp" />

3)使用您自己的ID值,但将其设置在ArrayAdapter

ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>(
        this, 
        R.layout.list_item, 
        R.id.item_title,   // for example
        employees);

4)定义您自己的扩展EmployeeAdapter

ArrayAdapter<Employee>

答案 1 :(得分:0)

我认为是因为您没有在employees.xml中定义命名空间,但是您有factory.setNamespaceAware(true);

试试这个:factory.setNamespaceAware(false);

答案 2 :(得分:0)

我错放了employees.xml文件。必须在项目的src / main下创建assets目录,在那里我应该放置我想要解析的xml文件。谢谢你们:))

相关问题