String jsonData="{\"sem1\" :[{\"subname\":\"TOC\",\"subcode\":\"1009\",\"subcredit\":\"6\",\"subgrade\":\"AB\"},{\"subname\":\"DS\",\"subcode\":\"10090\",\"subcredit\":\"5\",\"subgrade\":\"BB\"},{\"subname\":\"TOC\",\"subcode\":\"1009\",\"subcredit\":\"6\",\"subgrade\":\"AB\"}],\"sem2\":[{\"subname\":\"AAS\",\"subcode\":\"111009\",\"subcredit\":\"6\",\"subgrade\":\"AB\"},{\"subname\":\"AE\",\"subcode\":\"103309\",\"subcredit\":\"6\",\"subgrade\":\"DD\"}]}";
这是我的代码:
protected String doInBackground(String... params1) {
try {
jsonObject = new JSONObject(jsondata);
} catch (JSONException e) {
e.printStackTrace();
}
Iterator<String> iter = jsonObject.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONArray jsonArray = jsonObject.getJSONArray(key);
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
subname = jsonObject1.getString("subname");
subcode = jsonObject1.getString("subcode");
subcredit = jsonObject1.getString("subcredit");
subgrade = jsonObject1.getString("subgrade");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
答案 0 :(得分:0)
创建一个列表视图并为其添加值。
private List<YourListcontainer> listContainer = new ArrayList<YourListcontainer>();
获取您的json值并将其添加到yourListContainer中。
MyListAdapter adapter = new MyListAdapter();
Listview lv = (ListView) rootView.findViewById(R.id.lvid);
lv.setAdapter(adapter);
private class MyListAdapter extends ArrayAdapter<YourListcontainer> {
public MyListAdapter() {
// calls the base class constructor.
super(context, R.layout.list_adapter, listContainer);
// context,resources,object
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemview = convertView;
LayoutInflater listView = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (itemview == null) {
itemview = listView.inflate(R.layout.list_adapter,
parent, false);
}
final YourListContainer item = listContainer.get(position);// returns the index of the cell
TextView subject = (TextView) itemview
.findViewById(R.id.txvsubject);
subject.setText(item.getSubjectname());
TextView code= (TextView) itemview
.findViewById(R.id.txvcode);
code.setText(item.getCode());
TextView credit = (TextView) itemview
.findViewById(R.id.txvcredit );
credit .setText(item.getCredit ());
TextView grade = (TextView) itemview
.findViewById(R.id.txvgrade );
grade .setText(item.getGrade ());
subject.setBackgroundResource(R.drawable.borderstyle);
code.setBackgroundResource(R.drawable.borderstyle);
credit.setBackgroundResource(R.drawable.borderstyle);
grade.setBackgroundResource(R.drawable.borderstyle);
return itemview;
}
}
注意:这些边框样式用于以表格形式绘制border.xml,如框类型。