2012-08-02 57 views
3

在我的Android应用程序中,我有一个包含ExpandableListView的活动。从XML文件填充ExpandableListView - Android

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

用户应该能够使用应用程序提供的功能添加,删除,编辑ExpandableListView中的项目,并对XML文件的内容进行修改。最终,应用程序会将修改的XML文件发送回服务器。

为了更好地理解这个样机应该解释我想实现:

enter image description here

我想知道:

how can I populate dynamically in Java the area highlighted in red given the XML file?

示例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> 

将DEBUG PART

我一直在努力,实现由@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);

的是什么,我做错了任何想法的?

+0

多大是xml来自服务器? – Luksprog 2012-08-02 07:37:47

+0

@Luksprog - 目前'XML'文件大小不是问题,假设它只有几个条目,如示例代码。我能知道你为什么问吗? – Matteo 2012-08-02 07:40:24

+0

如果大小不是问题,那么它很简单。从服务器获取xml->构建一个结构来复制xml->将其显示给列表中的用户 - >用户修改该结构 - >在有效的xml中解析被修改的结构 - >将其发送到服务器。如果xml的大小很大,则不能执行上述操作,因为可能会导致内存不足。 – Luksprog 2012-08-02 07:46:18

回答

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

} 
+0

我看,看起来很简单,在我尝试之前只有一个疑问:我应该填写哪些方法存根? – Matteo 2012-08-02 08:18:22

+0

@Matteo如果像我一样扩展BaseExpandableListAdapter,你必须实现所有方法,因为所有方法都需要实现。如果扩展其他适配器,则至少应扩展返回组/子项数的方法以及组/子项的实际数据以返回适当的数据。 – Luksprog 2012-08-02 08:21:13

+0

非常感谢您的建议和明确的解释!我现在就试试,尽快给您反馈! ; D干杯... – Matteo 2012-08-02 08:23:11