AND*_*849 13 java arrays android arraylist multidimensional-array
我有一个用户列表ExpandableListView,现在我有2组列表,现在我正在尝试创建一个ArrayList将添加数据,因为我点击用户,所以如果有2组学校,我点击一个每个人的学生我应该在我的阵列中有2个位置,每个组包含其各自的用户一个,我的问题是,我的阵列有2个位置,但它不是分开学生:
我想要的是这个:
学校A:
student1被选中
STUDENT2
student3入选
学校B:
student4
student5入选
结果如下:
[0] - >学生1,3 [1] - >学生5
这是我到目前为止尝试的内容:
mGpsEscolas = new GPSEscolas();
mArrayEscolas = new ArrayList<GPSEscolas>();
aMap = new HashMap<String, GPSEscolas>();
ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(final ExpandableListView parent, View v, final int groupPosition, final int childPosition, final long id) {
ExpAdapter.setClicked(groupPosition, childPosition);
index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
parent.setSelectedChild(groupPosition, childPosition, true);
parent.getChildAt(index);
IdAlunos = String.valueOf(mMainRest.mArrayList.get(groupPosition).getalunos().get(childPosition).getId_aluno());
IdEscola = String.valueOf(mMainRest.mArrayList.get(groupPosition).getId_escola());
ids_alunos.add(IdAlunos);
notificar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int groupCount = ExpandList.getExpandableListAdapter().getGroupCount();
for (int group = 1; group <= groupCount; group++) {
int gcount = ExpandList.getExpandableListAdapter().getChildrenCount(groupPosition);
mArrayEscolas = new ArrayList<GPSEscolas>();
for (int child = 1; child <= gcount; child++) {
mGpsEscolas.setIds_alunos(String.valueOf(IdAlunos).substring(1));
mGpsEscolas.setId_escola(Integer.valueOf(IdEscola));
mGpsEscolas.setLatitude(latitudeEscola);
mGpsEscolas.setLongitude(longitudeEscola);
mGpsEscolas.setDistancia(mMainRest.RaioEscola);
mArrayEscolas.add(mGpsEscolas);
if (ExpAdapter.isChildSelectable(groupPosition, childPosition)) {
aMap.put(ExpandList.getExpandableListAdapter().getChildId(group, child), mArrayEscolas);
}
}
}
}
});
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
还有一条路要走。Map<group,List<child>> selected items您可以通过侦听此事件以几乎相同的方式创建项目并将其添加到此列表,而不是创建包装对象:
mExpandableList.setOnChildClickListener(new OnChildClickListener() {
@Override public boolean onChildClick(ExpandableListView parent,
View v,int groupPosition, int childPosition, long id) {
//toggle selections code here
}
Run Code Online (Sandbox Code Playgroud)
所以这里是重要的部分:(以及完整的工作github 存储库)
@Override
protected void onCreate(Bundle savedInstanceState) {
...// some init part
final MyAdapter adapter = new MyAdapter(schools, students);
listView.setAdapter(adapter);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent,
View v, int groupPosition, int childPosition, long id) {
adapter.toggleSelection(groupPosition, childPosition);
adapter.notifyDataSetInvalidated();
return false;
}
});
}
Run Code Online (Sandbox Code Playgroud)
我们有几个选项来放置此类选定的项目地图,但在我的项目中,我在自定义适配器类中使用它。没有真正需要定制适配器,并且可以Map<G, List<C>> selectedItems;在Activity中放置相关函数(toggleSelection、isSelected、getSelectedItems),但我们仍然需要突出显示选定的销售,因此适配器通常是放置它的最佳位置。
private class MyAdapter<G, C> extends BaseExpandableListAdapter {
private List<G> groups;
private Map<G, List<C>> childMap;
private Map<G, List<C>> selectedItems;
public MyAdapter(List<G> groups, Map<G, List<C>> childMap){
this.groups = groups;
this.childMap = childMap;
this.selectedItems = new HashMap<>();
}
public boolean isSelected(int groupPosition, int childPosition){
G group = groups.get(groupPosition);
// getChild is adapter Fn and is the same as
// G group = groups.get(groupPosition)
// C child = childMap.get(group).get(childPosition);
C child = getChild(groupPosition, childPosition);
List<C> sel = selectedItems.get(group);
return sel != null && sel.contains(child);
}
public void toggleSelection(int groupPosition, int childPosition){
G group = groups.get(groupPosition);
C child = getChild(groupPosition,childPosition);
List<C> sel = selectedItems.get(group);
if (sel == null){
sel = new ArrayList<>(); // Lasy arrays creation
//can init all arrays in constructor and never check for nulls
selectedItems.put(group, sel);
}
if (sel.contains(child)) sel.remove(child);
else sel.add(child);
}
... // Adapter fns can find in git repo
Run Code Online (Sandbox Code Playgroud)

将结果 Map 转换为 List 将是一项简单的任务:
private ArrayList<String> selectedAsList(Map<String, List<String>> selectedItems){
ArrayList<String> result = new ArrayList<>();
for(List<String> students: selectedItems.values())
result.addAll(students);
return result;
}
Run Code Online (Sandbox Code Playgroud)
或类似的东西。
附言。您还可以使用它Map<group,List<child>>。它几乎可以是您想要的任何数据结构。如果组数据中没有重复项,则可以使用 2 个数组,也可以使用 1 个单个数组。你可以控制它,限制选择的数量等等......