利用json解析得到的数据动态加载界面
有时候,需要根据服务器返回的json数据动态的加载界面,如:
服务器返回的结果如:

public class DoctTitle {
public int code;
public String errors;
public String message;
public List<Result> results;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getErrors() {
return errors;
}
public void setErrors(String errors) {
this.errors = errors;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public static class Result {
public String dictId; //职称id
public String dictName;//职称名称
public String getDictId() {
return dictId;
}
public void setDictId(String dictId) {
this.dictId = dictId;
}
public String getDictName() {
return dictName;
}
public void setDictName(String dictName) {
this.dictName = dictName;
}
}
}
在onSuccess方法中解析并动态加载界面:
public void onSuccess(String str) {
if (str != null) {
int intCode = -1;
String strMessage = null;
mDoctTitle = new DoctTitle();
try {
JSONObject jsonObj = new JSONObject(str);
if (jsonObj != null) {
intCode = jsonObj.optInt("code");
strMessage = jsonObj.optString("message");
mDoctTitle.code = intCode;
mDoctTitle.message = strMessage;
}
if ("1".equals(mDoctTitle.code + "")) {
JSONArray jsonTitleResults = jsonObj.optJSONArray("results");
List<DoctTitle.Result> titleResults;
if (jsonTitleResults != null && jsonTitleResults.length() > 0) {
titleResults = new ArrayList<DoctTitle.Result>();
for (int i = 0; i < jsonTitleResults.length(); i++) {
JSONObject jsonTempTitle = jsonTitleResults.getJSONObject(i);
DoctTitle.Result tempTitle = new DoctTitle.Result();
tempTitle.dictId = jsonTempTitle.optString("dictId");
tempTitle.dictName = jsonTempTitle.optString("dictName");
//动态加载展示职称列表的布局
loadDoctTitleLayout(tempTitle);
}
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
}
loadDoctTitleLayout方法:
private void loadDoctTitleLayout(DoctTitle.Result tempTitle) {
LinearLayout.LayoutParams RL_MW = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final TextView tvDoctTitle = new TextView(getApplicationContext());
tvDoctTitle.setLayoutParams(RL_MW);
tvDoctTitle.setGravity(Gravity.CENTER);
tvDoctTitle.setPadding(0, 23, 0, 23);
tvDoctTitle.setTag(tempTitle.dictId);
tvDoctTitle.setText(tempTitle.dictName);
tvDoctTitle.setTextColor(getResources().getColor(R.color.active_data_submit));
tvDoctTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
tvDoctTitle.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
tvDoctTitle.setBackgroundColor(getResources().getColor(R.color.active_button_gray));
break;
case MotionEvent.ACTION_UP:
tvDoctTitle.setBackgroundColor(getResources().getColor(R.color.white));
break;
default:
break;
}
return false;
}
});
tvDoctTitle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
doctGrade = view.getTag().toString();
tvTitleContent.setText(((TextView) view).getText());
}
});
llDoctTitleContent.addView(tvDoctTitle);
}
浙公网安备 33010602011771号