Android的数据提供了json和XML方面的数据提供给我们,那么我们如果来解析他们呢,下面我们用到了谷歌提供的Gosn来解析。
ExpandableListView和listVIew有些不同,不同之处在于ExpandableListview提供两个数据源给我们,我们假设为group、childs.
//我们用到的json字符串。
[{"name":"第二议题","jFiles":[{"id":"019da5cf5c0f4d9aaa2e61ac701ddb96","name":"工程师管理和考核需求的分析与方案.docx","topicId":"5b81e764503e4b26b5cf4aa59a9b5e10","directory":"cf40078e70d449359bdc4d18af20ce5b\\5b81e764503e4b26b5cf4aa59a9b5e10","extensionName":"docx"},{"id":"2c635ef400494fbbac410f44f5b07daf","name":"Sany_analyses_old.docx","topicId":"5b81e764503e4b26b5cf4aa59a9b5e10","directory":"cf40078e70d449359bdc4d18af20ce5b\\5b81e764503e4b26b5cf4aa59a9b5e10","extensionName":"docx"}]},{"name":"议题关于","jFiles":[{"id":"001e097c151d4140bf1996b75b097670","name":"评测.docx","topicId":"d90c4e1357144fd88b02ae9a63d9cbe9","directory":"cf40078e70d449359bdc4d18af20ce5b\\d90c4e1357144fd88b02ae9a63d9cbe9","extensionName":"docx"},{"id":"e840b4aa38894c98b40deff6040ae4b5","name":"星沙_second_v.docx","topicId":"d90c4e1357144fd88b02ae9a63d9cbe9","directory":"cf40078e70d449359bdc4d18af20ce5b\\d90c4e1357144fd88b02ae9a63d9cbe9","extensionName":"docx"}]}]
我们可以看到【name:第二议题】装进一个List<String>group; 然后就是group下面的分组了【jFiles:[.... ]】并且看到里面用到哪些元素,我们就可以定义实体类了。
childs实体类:
public class JFiles {public String id; //文件idpublic String name; //文件名public String topicId; public String password; //public String directory;public String extensionName;public JFiles(String id, String name, String password,String directory, String extensionName) {super();this.id = id;this.name = name;//this.topicId = topicId;this.password = password;this.directory = directory;this.extensionName = extensionName;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTopicId() {return topicId;}public void setTopicId(String topicId) {this.topicId = topicId;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getDirectory() {return directory;}public void setDirectory(String directory) {this.directory = directory;}public String getExtensionName() {return extensionName;}public void setExtensionName(String extensionName) {this.extensionName = extensionName;}}
下面的List<JFiles>jFiles类型是Jfilespublic class MeetFile {private String name;private List<JFiles>jFiles;public String getName() {return name;}public void setName(String name) {this.name = name;}public List<JFiles> getjFiles() {return jFiles;}public void setjFiles(List<JFiles> jFiles) {this.jFiles = jFiles;}public MeetFile(String name) {super();this.name = name;}}实体类已经有了,可以开始利用Gson解析了,并添加数据源。
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<MeetFile>>() {数据源也已经拿到手了,接下来我们可以定义Adapter了,可以照着写.
}.getType();
Gson gson = new GsonBuilder().create();
List<MeetFile> meetFiles = (List<MeetFile>)gson.fromJson(json,type);
List<String>group = new ArrayList<String>();//定义group
List<List<JFiles>>items = new ArrayList<List<JFiles>>();里面的List<JFiles>每个group下面对应的childs
for (MeetFile meetFile : meetFiles) {//对解析出来的数据进行一个遍历操作
group.add(meetFile.getName());
List<JFiles> jFiles = meetFile.getjFiles();所有的childs数据
List<JFiles>childlist = new ArrayList<JFiles>();
for (JFiles jFile : jFiles) {
childlist.add(new JFiles(jFile.getId(),jFile.getName(), jFile.getPassword(),jFile.getDirectory(),jFile.getExtensionName()));
}
tems.add(childlist);//添加一个childs集合。
}
public class MeetFileAdapter extends BaseExpandableListAdapter{最后一步,ExpandableListView设置Adapter就可以了。
private List<String>group;//Adapter数据源Group
private List<List<JFiles>> childs;//Adapter数据源Item
private Context context;
public MeetFileAdapter(List<String> group, List<List<JFiles>> items,
Context context) {
super();
this.group = group;
this.childs = items;
this.context = context;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childs.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupHodler hodler;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_group, null);
hodler = new GroupHodler();
hodler.NumName = (TextView) convertView.findViewById(R.id.layout_tv);
convertView.setTag(hodler);
}else {
hodler = (GroupHodler) convertView.getTag();
}
hodler.NumName.setText(group.get(groupPosition));
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ChildHodler hodler;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_child, null);
hodler = new ChildHodler();
hodler.FileFormat = (ImageView) convertView.findViewById(R.id.IMG_FileFormet);
hodler.FileName = (TextView) convertView.findViewById(R.id.Tv_FileName);
convertView.setTag(hodler);
}else {
hodler = (ChildHodler) convertView.getTag();
}
JFiles childlist = childs.get(groupPosition).get(childPosition);
hodler.FileName.setText(childlist.getName());
String format = childlist.getExtensionName();
if (format.equals("doc")||format.equals("docx")) {
hodler.FileFormat.setImageResource(R.drawable.doc);
}else if (format.equals("xls")||format.equals("xlsx")) {
hodler.FileFormat.setImageResource(R.drawable.xls);
}else if (format.equals("ppt")||format.equals("pptx")) {
hodler.FileFormat.setImageResource(R.drawable.ppt);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
class GroupHodler{
TextView NumName;
}
class ChildHodler{
ImageView FileFormat;
TextView FileName;
}
}
ExpandableListView centerExlist = (ExpandableListView) .findViewById(R.id.XXX);
centerExlist.setAdapter(new MeetFileAdapter(group, items, XXX.this));
OK,服务器返回Json与Android端的解析就完成了,本人新手,希望可以多加指教一二。