从XML文件填充一个可扩展列表视图 - Android

3
在我的Android应用程序中,我有一个包含ExpandableListView的Activity。
它将包含的项目应从应用程序在启动时查询到服务器的XML文件中提取(假设文件大小不是问题)。
然后,用户应该能够通过使用应用程序提供的功能向ExpandableListView添加、删除、编辑项目来修改XML文件的内容。最终,应用程序将把修改后的XML文件发送回服务器。
为了更好地理解我要实现的内容,这个模拟应该解释清楚:
我想知道:
如何在Java中动态填充红色区域,给定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);

有什么想法吗?我做错了什么吗?


从服务器返回的XML有多大? - user
@Luksprog - 目前 XML 文件大小不是问题,假设它只有像示例代码中那样的几个条目。我可以知道你为什么问吗? - Matteo
如果大小不是问题,那么很简单。从服务器获取XML->构建结构以复制XML->在列表中向用户显示它->用户修改该结构->将修改后的结构解析为有效的XML->将其发送到服务器。如果XML文件太大,则无法执行上述操作,因为可能会耗尽内存。 - user
@Luksprog - 无论如何,您能否详细解释一下您提出的解决方案?“构建一个结构来复制xml”是可以的,但如何在列表中向用户展示它呢? - Matteo
@Luksprog - 我已经实现了你的解决方案,但是它给我带来了一些问题,你能否进一步帮助我?提前感谢! - Matteo
显示剩余4条评论
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;
    }
}

作为一种数据结构,使用解析xml后得到的ArrayList<Category>在自定义适配器中使用:
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;
    }

}

我明白了,看起来很简单,但在尝试之前有一个疑问:我应该填哪个方法存根? - Matteo
@Matteo 如果你像我一样扩展了BaseExpandableListAdapter,那么你必须实现所有方法,因为它们都是必须实现的。如果你扩展其他适配器,你至少应该扩展返回组/子项数量和实际数据的方法,以返回正确的数据。 - user
非常感谢您的建议和清晰的解释!我现在会立即尝试并尽快给您反馈!;D 干杯... - Matteo
我已经实现了你的解决方案,但是它出现了一些问题,你能否再帮我一下?先谢谢了! - Matteo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接