Java 从XML文件填充ExpandableListView-Android

Java 从XML文件填充ExpandableListView-Android,java,android,xml,android-layout,expandablelistview,Java,Android,Xml,Android Layout,Expandablelistview,在我的Android应用程序中,我有一个包含 它将包含的项目应该从应用程序启动时向服务器查询的XML文件中提取(假设文件的大小不是问题) 然后,用户应该能够使用应用程序提供的功能,通过添加、删除和编辑ExpandableListView中的项目来修改XML文件的内容。最后,应用程序将把修改后的XML文件发送回服务器 为了更好地理解此模型,应解释我试图实现的内容: 我想知道: 如何在Java以红色突出显示的区域中动态填充 给定XML文件 示例XML文件: <?xml version="1.

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

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

然后,用户应该能够使用应用程序提供的功能,通过添加、删除和编辑
ExpandableListView
中的项目来修改
XML
文件的内容。最后,应用程序将把修改后的
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>
错误:

注意

com.DVA\u HLUI.DVA\u HLUIManageTaxonomyActivity.onCreate(DVA\u HLUIManageTaxonomyActivity.java:80)

对应于代码的这一行

myCustomExpandableListView.setAdapter(LabeltaxOnMyAdapter)


你知道我做错了什么吗?

我会制作一个自定义类,如下所示:

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;
    }
}
类别{
private ArrayList mEntries=new ArrayList();
私有字符串mCaTegoryName;
公共无效补遗(字符串条目){
添加(条目);
}
公共无效setCategoryName(字符串categoryName){
McCategoryName=categoryName;
}
公共ArrayList getEntries(){
返回指令;
}
公共字符串getCategoryName(){
返回mCaTegoryName;
}
}
作为数据结构,并使用将在自定义适配器中解析xml所产生的
ArrayList

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

}
class CustomExpandable扩展了BaseExpandableListAdapter{
私有数组列表;
公共自定义可扩展(上下文、ArrayList项){
mItems=项目;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回mItems.get(groupPosition.getEntries().get(childPosition);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回0;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、,
布尔值isLastChild、视图转换视图、视图组父级){
//实现子视图行
返回null;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回mItems.get(groupPosition.getEntries().size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回mItems.get(groupPosition.getCategoryName();
}
@凌驾
public int getGroupCount(){
返回mItems.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回0;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
//实现组视图
返回null;
}
@凌驾
公共布尔表ID(){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
返回true;
}
}

来自服务器的xml有多大?@Luksprog-目前
xml
文件大小不是问题,假设它只有一些条目,如示例代码中所示。我能知道你为什么问吗?如果尺寸不是问题,那就很简单了。从服务器获取xml->构建复制xml的结构->将其显示给列表中的用户->用户修改该结构->在有效xml中解析修改后的结构->将其发送到服务器。如果xml的大小太大,您就无法执行上述操作,因为您可能会耗尽内存。@Luksprog-是的,我知道,但如果大小太大,我想建立一个本地数据库和一些策略,只将本地数据库中发生的修改发送到服务器,反之亦然。你觉得这个解决方案怎么样?@Luksprog-无论如何,你能详细解释一下你提出的解决方案吗<代码>构建一个复制xml的结构
是可以的,但是如何做到这一点
将它显示给列表中的用户
?我明白了,这看起来很简单,在我尝试之前有一个疑问:我应该填写哪一个方法存根?@Matteo如果你像我一样扩展
BaseExpandableListAdapter
,你必须实现所有方法,因为所有方法都需要实现。如果您扩展了其他适配器,那么您至少应该扩展返回组/子级数量和组/子级实际数据的方法,以返回正确的数据。Thk关于建议和清晰的解释,请参阅!我现在就试试,尽快给你反馈;D干杯…我已经实施了你的解决方案,但它给了我一些问题,你能帮我进一步吗?提前!
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;
    }
}
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;
    }

}