【发布时间】:2018-10-13 22:23:27
【问题描述】:
我遇到了可展开列表视图的问题。我见过的所有关于父项的教程都只有一项。就我而言,我正在处理父母和孩子中的多个字段(名称和评论)。我应该如何进行?
我的 json 数据
[
{
nome: "Carlos",
comentario: "Esse dia foi show",
resp: [ ]
},
{
nome: "Andre",
comentario: "Acho que não precisava disso",
resp: [
{
nome: "inutil",
comentario: "Sempre assim"
},
{
nome: "Roberto",
comentario: "Se não estivessem fazendo nada errado, não iam querer apagar a mídia."
},
{
nome: "xumbinho",
comentario: "André ! Para vai!"
}
]
},
{
nome: "Celso",
comentario: "É pra acabar mesmo",
resp: [ ]
}
]
还有我的评论活动代码
private void makejsonobjreq() {
PD.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url+"284901",
null, response -> {
ArrayList<Group> list = new ArrayList<Group>();
ArrayList<Child> ch_list;
Log.d("response", String.valueOf(response));
try {
Iterator<String> key = response.keys();
while (key.hasNext()) {
String k = key.next();
Log.d("KEY", String.valueOf(k));
Group gru = new Group();
gru.setNome(k);
ch_list = new ArrayList<Child>();
JSONArray ja = response.getJSONArray(k);
Log.d("QNT", String.valueOf(ja.length()));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
Child ch = new Child();
Log.d("COMENTARIO NOME",jo.getString("nome"));
ch.setNome(jo.getString("nome"));
ch.setComentario(jo.getString("comentario"));
ch_list.add(ch);
} // for loop end
gru.setItems(ch_list);
list.add(gru);
} // while loop end
ExpAdapter = new ExpandListAdapter(ComentariosActivity.this, list);
ExpandList.setAdapter(ExpAdapter);
PD.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
PD.dismiss();
Log.d("response", String.valueOf(error));
}
});
MyApplication.getInstance().addToRequestQueue(jsonObjReq, "jreq");
}
适配器
package br.inf.cgn.cgnapp;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import br.inf.cgn.cgnapp.model.Child;
import br.inf.cgn.cgnapp.model.Group;
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Group> groups;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.comentarios_resp, null);
}
if (imageLoader == null)
imageLoader = MyApplication.getInstance().getImageLoader();
TextView nome = (TextView) convertView.findViewById(R.id.nome_resp);
nome.setText(child.getNome().toString());
TextView comentario = (TextView) convertView.findViewById(R.id.comentario_resp);
comentario.setText(child.getComentario().toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.comentarios_list, null);
}
TextView nome = (TextView) convertView.findViewById(R.id.nome);
nome.setText(group.getNome());
TextView comentario = (TextView) convertView.findViewById(R.id.comentario);
comentario.setText(group.getComentario());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我尝试了几种方法来使这段代码工作,但都没有成功!
【问题讨论】:
-
能否请您发布 ExpandListAdapter 的代码。谢谢!
-
当然!非常感谢link。
标签: java android expandablelistview expandablelistadapter