从填充XML文件的ExpandableListView - Android电子 [英] Populate an ExpandableListView from XML file - Android

查看:121
本文介绍了从填充XML文件的ExpandableListView - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我已经得到了包含 ExpandableListView

In my Android application I've got an Activity containing an ExpandableListView.

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

The items it will contain should be extracted from an XML file that the application will query to a Server when it starts (suppose the size of the file is not an issue).

然后,用户应该能够通过添加,删除,编辑从 ExpandableListView XML 文件的内容code>与应用程序提供的功能。最终,应用程序将发回修改后的 XML 文件到服务器。

The user should then be able to make modifications to the content of the XML file by adding, removing, editing items from the ExpandableListView with functionalities provided by the application. Eventually the application will send back the modified XML file to the Server.

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

For better understanding this mockup should explain what I'm trying to implement:

我想知道:

我怎么能在 Java的动态填充的区域以红色突出显示
  鉴于 XML 文件?

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>


ADDED DEBUG PART

我试图实现由@Luksprog答案提出了解决方案,但现在面临一个显示java.lang.NullPointerException 运行以下code时:

I've tried to implement the solution proposed in the answer by @Luksprog but am facing a java.lang.NullPointerException when running the following code:

code:

//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)

注意

<$c$c>com.DVA_HLUI.DVA_HLUIManageTaxonomyActivity.onCreate(DVA_HLUIManageTaxonomyActivity.java:80)

对应于该行的code的

corresponds to this line of the code

myCustomExpandableListView.setAdapter(labelTaxonomyAdapter);

我做错了任何想法?

推荐答案

我会做一个自定义类是这样的:

I would make a custom class like this:

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&LT;分类和GT; ,这将导致在自定义适配器解析XML:

as a data structure and use the ArrayList<Category> that will result from parsing the xml in a custom adapter:

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

}

这篇关于从填充XML文件的ExpandableListView - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆