如何存储在一个ArrayList的ExpandableListView的所选项目 [英] How to store selected items of an ExpandableListView in an ArrayList
问题描述
我有用户内部的名单 ExpandableListView
,现在我已经2组名单,现在我想要创建一个的ArrayList
,将增加数据,我点击的用户,因此,如果有2组的学校,我点击每一个学生,我应该有2个职位,我的数组,一个包含每个组的各个用户,我的问题是,我的阵列有2个位置,但它不是分离的学生:
我想是这样的:
学校答:
student1选择
STUDENT2
学生三选择
学校B:
student4
student5选择
造成这种:
[0] - >学生1,3 [1] - >学生5
下面是我试过到目前为止:
mGpsEscolas =新GPSEscolas();
mArrayEscolas =新的ArrayList< GPSEscolas>();
AMAP =新的HashMap<字符串,GPSEscolas>();
ExpandList.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){
@覆盖
公共布尔onChildClick(最终ExpandableListView父,视图V,最终诠释groupPosition,最终诠释childPosition,最终长ID){
ExpAdapter.setClicked(groupPosition,childPosition);
指数= parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition,childPosition));
parent.setItemChecked(指数,真正的);
parent.setSelectedChild(groupPosition,childPosition,真正的);
parent.getChildAt(指数);
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(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
INT groupCount = ExpandList.getExpandableListAdapter()getGroupCount()。
对于(INT组= 1;组< = groupCount;组++){
INT gcount的= ExpandList.getExpandableListAdapter()getChildrenCount(groupPosition)。
mArrayEscolas =新的ArrayList< GPSEscolas>();
对于(INT孩子= 1;子< = gcount的;孩子++){
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);
如果(ExpAdapter.isChildSelectable(groupPosition,childPosition)){
aMap.put(ExpandList.getExpandableListAdapter()getChildId(组,儿童),mArrayEscolas);
}
}
}
}
});
返回false;
}
});
有走多了一个途径。而不是创建包装对象,你可以创建地图<组名单,其中,儿童>>选择项目
和添加项目到这个列表pretty的大致相同的方式,通过听此事件:
mExpandableList.setOnChildClickListener(新OnChildClickListener(){
@覆盖公共布尔onChildClick(ExpandableListView父母,
视图V,INT groupPosition,诠释childPosition,长ID){
//切换选择code在这里
}
因此,这里是重要的组成部分:(和完整的工作<一href="https://github.com/varren/ExpandableListViewMultipleSelection/blob/master/app/src/main/java/com/varren/expandablelistviewmultipleselections/MainActivity.java"相对=nofollow> GitHub库)
@覆盖
保护无效的onCreate(包savedInstanceState){
... //一些初始化的一部分
最后MyAdapter适配器=新MyAdapter(学校,学生);
listView.setAdapter(适配器);
listView.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){
@覆盖
公共布尔onChildClick(ExpandableListView父母,
视图V,INT groupPosition,诠释childPosition,长ID){
adapter.toggleSelection(groupPosition,childPosition);
adapter.notifyDataSetInvalidated();
返回false;
}
});
}
我们在哪里把这种选择的项目地图有多种选择,但在我的项目,我在自定义适配器类使用它。没有用于custome适配器没有真正的需要,并有可能把地图&LT; G,名单,其中,C&GT;&GT; selectedItems;
及相关功能(toggleSelection,isSelected,getSelectedItems)的活动,但我们仍然需要突出选择出售,所以适配器通常是把它最好的地方
私有类MyAdapter&LT; G,C&GT;扩展BaseExpandableListAdapter {
私人列表&LT; G&GT;组;
私人地图&LT; G,名单,其中,C&GT;&GT; childMap;
私人地图&LT; G,名单,其中,C&GT;&GT; selectedItems;
公共MyAdapter(名单&LT; G&基团,地图&LT; G,名单,其中,C&GT;&GT; childMap){
this.groups =组;
this.childMap = childMap;
this.selectedItems =新的HashMap&LT;&GT;();
}
公共布尔isSelected(INT groupPosition,诠释childPosition){
G团= groups.get(groupPosition);
// getChild是适配器Fn的同时是相同的
// G团= groups.get(groupPosition)
// C子= childMap.get(组)获得(childPosition);
C子元素= getChild(groupPosition,childPosition);
名单&LT; C&GT; SEL = selectedItems.get(组);
返回SEL = NULL和放大器;!&安培; sel.contains(子);
}
公共无效toggleSelection(INT groupPosition,诠释childPosition){
G团= groups.get(groupPosition);
C子元素= getChild(groupPosition,childPosition);
名单&LT; C&GT; SEL = selectedItems.get(组);
如果(SEL == NULL){
SEL =新的ArrayList&LT;&GT;(); //镭石光电阵列创建
//可以初始化所有数组在构造函数中,从来没有检查空值
selectedItems.put(组,SEL);
}
如果(sel.contains(子))sel.remove(子);
其他sel.add(子);
}
... //适配器FNS可混帐回购找到
和转换结果地图列表将是一件容易的事:
私人的ArrayList&LT;字符串&GT; selectedAsList(地图&LT;字符串列表与LT;字符串&GT;&GT; selectedItems){
ArrayList的&LT;字符串&GT;结果=新的ArrayList&LT;&GT;();
对于(名单&LT;字符串&GT;学生:selectedItems.values())
result.addAll(学生);
返回结果;
}
或类似的东西。
PS。组列表与LT;您也可以使用地图&LT打孩子&GT;&GT;
。它可以是pretty的多任
你希望它是数据结构。 2阵列或者只是1单一阵列中,如果你没有在你的小组数据副本。你可以控制它,限制了选择的数量等等...
I have a list of users inside of an ExpandableListView
, for now I have 2 groups of list, now I'm trying to create an ArrayList
that will add data as I click on the users, so if have 2 groups of schools and I click on a student of each one I should have 2 positions in my array, one for each group containing its respective users, my problem is, my array has 2 positions but it is not separating the students:
What I want is this:
School A:
student1 selected
student2
student3 selected
School B:
student4
student5 selected
Resulting in this:
[0]-> student 1,3 [1] ->student 5
Here is what I tried so far:
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;
}
});
There is one more way to go. Instead of creating wrapper object you can create Map<group,List<child>> selected items
and add items to this list pretty much the same way, by listening this event:
mExpandableList.setOnChildClickListener(new OnChildClickListener() {
@Override public boolean onChildClick(ExpandableListView parent,
View v,int groupPosition, int childPosition, long id) {
//toggle selections code here
}
So here is important part: (and full working github repo )
@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;
}
});
}
We have several options where to put such selected items map, but in my project I use it in custom adapter class. There is no real need for custome adapter, and it is possible to put Map<G, List<C>> selectedItems;
and related functions (toggleSelection, isSelected, getSelectedItems) in Activity, but we still need to highlight selected sells, so adapter usually is the best place to put it.
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
And convert result Map to List will be an easy task:
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;
}
or something similar.
PS. You can also play with Map<group,List<child>>
.It can be pretty much any
data structure you want it to be. 2 arrays or maybe just 1 single array if you don't have duplicates in your group data. You can control it, limit the number of selections and so on...
这篇关于如何存储在一个ArrayList的ExpandableListView的所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!