2012-08-02 83 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