Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SimpleExpandableListAdapter的数据应该如何组织_Android - Fatal编程技术网

Android SimpleExpandableListAdapter的数据应该如何组织

Android SimpleExpandableListAdapter的数据应该如何组织,android,Android,我正在尝试使用SimpleExpandableListAdapter创建一个ExpandableListView(我知道我可以自己扩展一个新适配器,但我想尝试simpleExapndableListAdapter) 现在,我在尝试创建SimpleExpandableListAdapter的新实例时遇到了问题 在阅读事件时,我不太确定参数的含义,尤其是groupData和childData 虽然我知道我必须创建一个Map列表和一个list of Map列表,但是应该将哪些数据放在那里呢?如何组织它

我正在尝试使用
SimpleExpandableListAdapter
创建一个
ExpandableListView
(我知道我可以自己扩展一个新适配器,但我想尝试simpleExapndableListAdapter)

现在,我在尝试创建
SimpleExpandableListAdapter
的新实例时遇到了问题

在阅读事件时,我不太确定参数的含义,尤其是
groupData
childData

虽然我知道我必须创建一个
Map
列表和一个
list of Map
列表,但是应该将哪些数据放在那里呢?如何组织它们

例如,这是我想要显示的数据,如何组织它们

++Development Team
  John
  Bill
++Data Process Team
  Alice
  David
顺便问一下,这是否意味着我必须为组和子视图创建两个布局

我在谷歌上搜索了一遍又一遍地阅读教程,但我不明白


我希望有人能给我一个解释。

对于你的要求,有这么多的例子

不管怎样,如果你还没有读过的话,你可能想看看这个博客

“这是否意味着我必须为组和子视图创建两个布局?”

当然,是的,我的意思是,你可以选择不创建布局,但这也是你的选择,这与ExpandableList关系不大

理想情况下,组列表将包含团队列表行列表={“开发团队”、“数据处理团队”}(这主要用于显示目的,如计算列表的高度等)

理想情况下,ChildList将包含列表列表(而不是列表的Hashmap)

Hashmap>=

开发团队: 列表={“约翰”,“比尔”} 数据处理小组:
List={“John”,“Bill”}

您将需要两个列表或数组

家长(开发团队和数据处理团队)

父项列表是常规列表,您必须添加所有父项,即顶级项

children

父列表和子列表之间的主要区别在于,子列表是一个列表列表

例如,如果你需要父母的孩子,你会这样做

children.get(parentPosition)
如果你需要一个特定的孩子

children.get(parentPosition).get(childPosition)

您可以使用
BaseExpandableListAdapter

找到一个示例,这是您想要的简单示例,它将消除您的所有疑问

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;

public class SimpleExpandableListExampleActivity extends ExpandableListActivity {
    private static final String NAME = "NAME";

    private ExpandableListAdapter mAdapter;

    private String group[] = {"Development" , "Data Process Team"};
    private String[][] child = { { "John", "Bill" }, { "Alice", "David" } };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
        for (int i = 0; i < group.length; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, group[i]);

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < child[i].length; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(NAME, child[i][j]);
            }
            childData.add(children);
        }

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(this, groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME }, new int[] { android.R.id.text1 },
                childData, android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME }, new int[] { android.R.id.text1 });
        setListAdapter(mAdapter);
    }

}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入android.app.ExpandableListActivity;
导入android.os.Bundle;
导入android.widget.ExpandableListAdapter;
导入android.widget.SimpleExpandableListAdapter;
公共类SimpleExpandableListExampleActivity扩展了ExpandableListActivity{
私有静态最终字符串NAME=“NAME”;
私有可扩展列表适配器mAdapter;
私有字符串组[]={“开发”,“数据处理团队”};
私有字符串[][]child={{“John”,“Bill”},{“Alice”,“David”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
List groupData=new ArrayList();
List childData=new ArrayList();
对于(int i=0;i
你所要做的就是

  • 使用
    SimpleExpandableListExampleActivity
    创建一个Android项目 你的主要活动
  • 复制粘贴该活动中给定的代码
  • 就这样。。运行你的代码

希望这有帮助……

父项列表是常规列表,您必须添加所有父项,即顶级项目
。我只是想知道为什么要列出地图?为什么不使用字符串列表?例如:
groups=newarraylist();groups.add(“group1”)?
如果扩展
BaseExpandableListAdapter
则可以使用任何类型的对象的
ArrayList
。我不知道为什么Android开发人员在
SimpleExpandableListAdapter
上使用
Map
的原因,第一个
children.get(parentPosition)
be
parent.get(parentPosition)
children.get(parentPosition)
将在
parentPosition
处返回父对象的子对象列表,因此如果需要父对象,您将执行
parentList.get(parentPosition)
操作。但是我想知道你是如何知道钥匙
名称的
以及你是如何知道id
R.id.text1
等等的?顺便说一句,我发现指示器与文本不一致,有没有办法调整它们?看看这个:你有没有注意到android.R.
使用了。。这意味着我使用了Android提供的布局,我没有创建它。。。您可以找到它是什么,只需按Ctrl+android.R.layout.simple\u expandable\u list\u item\u 1您将在那里找到名为text1的
TextView
。。。至于与文本对齐的指示器,,,你们在模拟器上测试吗??,如果是,那个么什么(我在这里不看到…),在真实的设备上检查。。。如果你想知道其他事情,请告诉我:)