Android ExpandableListview使用gson解析樹形結構資料。
阿新 • • 發佈:2019-02-02
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; //檔案id public 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; } }
實體類已經有了,可以開始利用Gson解析了,並新增資料來源。下面的List<JFiles>jFiles型別是Jfiles public 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; } }
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<MeetFile>>() {
}.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集合。
}
資料來源也已經拿到手了,接下來我們可以定義Adapter了,可以照著寫.
public class MeetFileAdapter extends BaseExpandableListAdapter{
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設定Adapter就可以了。
ExpandableListView centerExlist = (ExpandableListView) .findViewById(R.id.XXX);
centerExlist.setAdapter(new MeetFileAdapter(group, items, XXX.this));
OK,伺服器返回Json與Android端的解析就完成了,本人新手,希望可以多加指教一二。