gpt4 book ai didi

java - 如何将 ExpandableListView 的选定项目存储在 ArrayList 中

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:28 25 4
gpt4 key购买 nike

我在 ExpandableListView 中有一个用户列表,现在我有 2 组列表,现在我正在尝试创建一个 ArrayList 来添加数据当我点击用户时,如果有 2 组学校并且我点击每组的一名学生,我应该在我的数组中有 2 个位置,每个包含其各自用户的组一个,我的问题是,我的数组有 2 个位置但它并没有把学生分开:

我想要的是:

学校A:

已选择学生 1

学生2

student3 已选中

乙校:

学生4

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

最佳答案

一个简单的解决方案是创建一个新类 SelectableObject:

class SelectableObject<T>
{
boolean sel; T obj;
public SelectableObject<T>(T obj) { this.obj=obj; this.sel=false; }
public void select() { this.sel=true; }
public void deselect() { this.sel=false; }
public boolean isSelected() { return this.sel; }
public T getObject() { return this.obj; }
}

然后像这样创建你的ExpandableListView

public void setChildData() 
{
ArrayList<SelectableObject<GPSEscolas>> child
= new ArrayList<SelectableObject<GPSEscolas>>();

child.add(new SelectableObject<GPSEscolas>(new GPSEscolas(..)));

..

childItems.add(child);
}

然后我们需要让onSelect监听函数调用select函数

mExpandableList.setOnChildClickListener(new OnChildClickListener() {
@Override public boolean onChildClick(ExpandableListView parent,
View v,int groupPosition, int childPosition, long id) {

SelectableObject<GPSEscolas> item = (SelectableObject<GPSEscolas>)
parent.getExpandableListAdapter().getChild(groupPosition,childPosition);

if(!item.isSelected()) item.select();
else item.deselect();

..

})

然后我们可以这样查询选中的item

public static ArrayList<GPSEscolas> getSelectedChildren(ExpandableListView listView)
{
ArrayList<GPSEscolas> list = new ArrayList<GPSEscolas>();

int count = listView.getGroupCount();

for (int group = 1; group <= count; group++)
{
int gcount = listView.getChildrenCount(position);

for (int child = 1; child <= gcount; child++)
{
SelectableObject<GPSEscolas> item = (SelectableObject<GPSEscolas>)
listView.getExpandableListAdapter().getChild(groupPosition,childPosition);

// Here is where you can see the solution beauty

if (item.isSelected())
{
list.add(item.getObject());
}
}
}

return list;
}

关于java - 如何将 ExpandableListView 的选定项目存储在 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com