簡體   English   中英

從XML文件填充ExpandableListView - Android

[英]Populate an ExpandableListView from XML file - Android

在我的Android應用程序中,我有一個包含ExpandableListView的Activity。

它將包含的項目應該從XML文件中提取,應用程序在啟動時將查詢到該文件(假設文件的大小不是問題)。

然后,用戶應該能夠通過添加,刪除,編輯ExpandableListView項目以及應用程序提供的功能來修改XML文件的內容。 最終,應用程序將修改后的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 );

我做錯了什么想法?

我會像這樣制作一個自定義類:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM