3

在我的 Android 应用程序中,我有一个包含ExpandableListView.

它将包含的项目应该从XML应用程序启动时向服务器查询的文件中提取(假设文件的大小不是问题)。

然后,用户应该能够通过从应用程序提供的功能XML中添加、删除、编辑项目来修改文件的内容。ExpandableListView最终,应用程序会将修改后的XML文件发送回服务器。

为了更好地理解这个模型应该解释我正在尝试实现的内容:

在此处输入图像描述

我想知道:

Java在给定文件的情况下,如何在以红色突出显示的区域中动态填充XML

示例 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<Category value="Animals">
    <entry>Cat</entry>
    <entry>Dog</entry>
    <entry>Elephant</entry>
</Category>
<Category value="Objects">
    <entry>Aeroplane</entry>
    <entry>Ball</entry>
    <entry>Closet</entry>
</Category>

添加调试部分

我试图实现@Luksprog 在答案中提出的解决方案,但java.lang.NullPointerException在运行以下代码时面临一个问题:

代码:

//gets the View from the Layout file
myCustomExpandableListView = (ExpandableListView) findViewById( R.id.myCustomExpandableListView );
//creates the array list that will contain all labels
ArrayList<Category> labelsInTaxonomy = new ArrayList<Category>();
//fills it with a private method that parses the XML and fills the array list
this.loadTaxonomyFromXml( labelsInTaxonomy );
//creates the custom expandable list adapter
CustomExpandable labelTaxonomyAdapter = new CustomExpandable( this, labelsInTaxonomy );
//sets the adapter
myCustomExpandableListView.setAdapter( labelTaxonomyAdapter );

错误:

E/AndroidRuntime( 5972): FATAL EXCEPTION: main
E/AndroidRuntime( 5972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DVA_HLUI/com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity}: java.lang.NullPointerException
E/AndroidRuntime( 5972):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
E/AndroidRuntime( 5972):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
E/AndroidRuntime( 5972):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 5972):    at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.main(ActivityThread.java:4196)
E/AndroidRuntime( 5972):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 5972):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 5972):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 5972):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 5972):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 5972): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 5972):    at com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity.onCreate(DVA_HLUIManageTaxonomyActivity.java:80)
E/AndroidRuntime( 5972):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
E/AndroidRuntime( 5972):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)

注意

com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity.onCreate(DVA_HLUIManageTaxonomyActivity.java:80)

对应这行代码

myCustomExpandableListView.setAdapter( labelTaxonomyAdapter );

关于我做错了什么的任何想法?

4

1 回答 1

2

我会做一个这样的自定义类:

class Category {

    private ArrayList<String> mEntries = new ArrayList<String>();
    private String mCaTegoryName;

    public void addEntry(String entry) {
        mEntries.add(entry);
    }

    public void setCategoryName(String categoryName) {
        mCaTegoryName = categoryName;
    }

    public ArrayList<String> getEntries() {
        return mEntries;
    }

    public String getCategoryName() {
        return mCaTegoryName;
    }
}

作为数据结构并使用ArrayList<Category>将在自定义适配器中解析 xml 的结果:

class CustomExpandable extends BaseExpandableListAdapter {

    private ArrayList<Category> mItems;

    public CustomExpandable(Context context, ArrayList<Category> items) {
        mItems = items;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mItems.get(groupPosition).getEntries().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // implement the child view row
        return null;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mItems.get(groupPosition).getEntries().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mItems.get(groupPosition).getCategoryName();
    }

    @Override
    public int getGroupCount() {
        return mItems.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // implement the group View
        return null;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}
于 2012-08-02T08:13:52.263 回答