gpt4 book ai didi

android - 在单个子行中包含多个数据的可扩展 ListView

转载 作者:行者123 更新时间:2023-11-29 19:15:41 26 4
gpt4 key购买 nike

我想显示组和子数据,我指的是这个链接 http://www.journaldev.com/9942/android-expandablelistview-example-tutorial ,但是本教程在 subview 和父 View 中只有一个项目,我想在父 View 和 subview 中设置多个项目。谁能帮我做这个?

for group 1)
name, location, number
for child 1)
name and id

我的 json 数据是,

"stds": [
{
"stopNum": "0",
"stopName": "one",
"loc": "23.097278,72.446139"
},
{
"stopNum": "1",
"stopName": "theatre",
"loc": "23.073028,72.5245",
"students": [
{
"studentName": "Luke",
"studentId": "58eb423bcf51a2bd759a740a"
}
]
},
{
"stopNum": "2",
"stopName": "flyover",
"loc": "23.07,72.521917",
"students": [
{
"studentName": "Leia",
"studentId": "58eb423ccf51a2bd759a740b"
}
]
},
{
"stopNum": "3",
"stopName": "mall",
"loc": "23.076222,72.50775",
"students": [
{
"studentName": "Advik",
"studentId": "58eb423ccf51a2bd759a740d"
},
{
"studentName": "Bipasha",
"studentId": "58eb423ccf51a2bd759a740e"
}
]
},
{
"stopNum": "4",
"stopName": "alpha",
"loc": "23.093639,72.495972",
"students": [
{
"studentName": "Aarav",
"studentId": "58eb423ccf51a2bd759a740c"
}
]
},
{
"stopNum": "5",
"stopName": "reliance",
"loc": "23.097278,72.446139",
"students": []
}
]

我是这样解析的,

String stopNum,stopName,stopLoc,studentName,studentId;
JSONArray routedataArray = generalFunc.getJsonArray(objects,"stds");

for (int i = 0; i < routedataArray.length(); i++) {
JSONObject jsonObject,studentobject;
JSONArray studentArray;
try {
jsonObject = routedataArray.getJSONObject(i);
stopNum=generalFunc.getStringFromJobject(jsonObject, "stopNum");
stopName=generalFunc.getStringFromJobject(jsonObject, "stopName");
stopLoc=generalFunc.getStringFromJobject(jsonObject, "loc");
studentArray=jsonObject.getJSONArray("students");

for(int j=0;j<studentArray.length();j++){
studentobject=studentArray.getJSONObject(j);
studentName=generalFunc.getStringFromJobject(studentobject, "studentName");
studentId=generalFunc.getStringFromJobject(studentobject, "studentId");
mapchild=new HashMap<>();
mapchild.put("studentName",studentName);
mapchild.put("studentId",studentId);
}

mapheader = new HashMap<>();
mapheader.put("stopNum",stopNum);
mapheader.put("stopName",stopName);
mapheader.put("loc",stopLoc);


} catch (JSONException e) {
e.printStackTrace();
}
}

最佳答案

这是一个工作示例,像这样创建模型类

public class routedata {
private String stopNum,stopName, stopLoc;
private ArrayList<studentData> ch_list;

public String getStopNum() {
return stopNum;
}

public String getStopName() {
return stopName;
}

public String getStopLoc() {
return stopLoc;
}


public ArrayList<studentData> getCh_list() {
return ch_list;
}

public routedata(String stopNum, String stopName, String stopLoc, ArrayList<studentData> ch_list) {
this.stopLoc=stopLoc;
this.stopName=stopName;
this.stopNum=stopNum;
this.ch_list=ch_list;
}

public ArrayList<studentData> getItems() {
return ch_list;
}
}

studentData.java

public class studentData {
String studentName,studentId;

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public String getStudentId() {
return studentId;
}

public void setStudentId(String studentId) {
this.studentId = studentId;
}

public studentData(String studentName, String studentId) {
this.studentId=studentId;
this.studentName=studentName;
}
}

像这样改变适配器..

public class RouteDataExpListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<routedata> list;


public RouteDataExpListAdapter(Context context, ArrayList<routedata> list) {
this.context=context;
this.list=list;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {

ArrayList<studentData> chList = list.get(listPosition).getItems();
return chList.get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

studentData child = (studentData) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_list_item, null);
}
TextView std_details = (TextView) convertView
.findViewById(R.id.expandedListItem);

std_details.setText(child.getStudentId()+" "+child.getStudentName());


return convertView;
}

@Override
public int getChildrenCount(int listPosition) {

ArrayList<studentData> chList = list.get(listPosition).getItems();
return chList.size();
}

@Override
public Object getGroup(int listPosition) {
return list.get(listPosition);
}

@Override
public int getGroupCount() {
return list.size();
}

@Override
public long getGroupId(int listPosition) {
return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

routedata group = (routedata) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_list_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.listTitle);
tv.setText(group.getStopNum()+" "+group.getStopName()+" "+group.getStopLoc());
return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}

最后像这样设置数据..

for (int i = 0; i < routedataArray.length(); i++) {
JSONObject jsonObject, studentobject;
JSONArray studentArray;
try {
jsonObject = routedataArray.getJSONObject(i);


ch_list = new ArrayList<studentData>();
stopNum = generalFunc.getStringFromJobject(jsonObject, "stopNum");
stopName = generalFunc.getStringFromJobject(jsonObject, "stopName");
stopLoc = generalFunc.getStringFromJobject(jsonObject, "loc");

studentArray = jsonObject.getJSONArray("students");

for (int j = 0; j < studentArray.length(); j++) {

studentobject = studentArray.getJSONObject(j);
studentName = generalFunc.getStringFromJobject(studentobject, "studentName");
studentId = generalFunc.getStringFromJobject(studentobject, "studentId");
studentData ch = new studentData(studentName,studentId);
ch_list.add(ch);

}
routedata gru = new routedata(stopNum,stopName,stopLoc,ch_list);
list.add(gru);

} catch (JSONException e) {
e.printStackTrace();
}
}

关于android - 在单个子行中包含多个数据的可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43584916/

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