我设计了一个自定义 BaseAdapter,它采用一个 Class(JsonKeys) 的 ArrayList,其中包含我需要的数据的 getter 和 setter。
我基本上是在解析一个 JSON 数组并将适当的数据插入到下面给出的列表行中:
"attendanceData": [
{
"ccode": "MAT 212",
"cname": "ENGINEERING MATHEMATICS- IV",
"cclass": "30",
"bunked": "5",
"percent": "86",
"updated": "3\/20\/13"
},
{
"ccode": "CSE 202",
"cname": "FORMAL LANGUAGES & AUTOMATA THEORY",
"cclass": "28",
"bunked": "3",
"percent": "91",
"updated": "3\/15\/13"
},
{
"ccode": "CSE 204",
"cname": "RELATIONAL DATABASE MANAGEMENT SYSTEMS",
"cclass": "24",
"bunked": "2",
"percent": "93",
"updated": "3\/15\/13"
},
{
"ccode": "CSE 206",
"cname": "MICROPROCESSORS",
"cclass": "30",
"bunked": "1",
"percent": "97",
"updated": "3\/6\/13"
},
{
"ccode": "CSE 208",
"cname": "EVENT DRIVEN PROGRAMMING USING JAVA",
"cclass": "32",
"bunked": "2",
"percent": "95",
"updated": "3\/14\/13"
},
{
"ccode": "OPE 1",
"cname": "OPEN ELECTIVE - ADVERTISING THEORY & PRACTICE (PME 332)",
"cclass": "13",
"bunked": "3",
"percent": "82",
"updated": "3\/21\/13"
},
{
"ccode": "CSE 210",
"cname": "MICROPROCESSORS LAB",
"cclass": "NA",
"bunked": "NA",
"percent": "NA",
"updated": "NA"
},
{
"ccode": "CSE 212",
"cname": "JAVA PROGRAMMING LAB",
"cclass": "NA",
"bunked": "NA",
"percent": "NA",
"updated": "NA"
},
{
"ccode": "CSE 214",
"cname": "RELATIONAL DATABASE MANAGEMENT SYSTEMS LAB",
"cclass": "NA",
"bunked": "NA",
"percent": "NA",
"updated": "NA"
}
]
这些是我的自定义 BaseAdapter 和分别膨胀列表视图的片段的类。
public class AttendanceListAdapter extends BaseAdapter
{
private ArrayList<JsonKeys> adapterkeylist = new ArrayList<JsonKeys>();
private Context context;
private static LayoutInflater inflater = null;
TextView subject, code, attended, absent, percent, updated;
JsonKeys dataString = new JsonKeys();
public AttendanceListAdapter(Context c, ArrayList<JsonKeys> jk)
{
context = c;
adapterkeylist =jk;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount()
{
return adapterkeylist.size();
}
@Override
public Object getItem(int position)
{
// return s[s.length/6][s.length%6];
return adapterkeylist.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (convertView == null)
v = inflater.inflate(R.layout.attendance_listrow, null);
subject = (TextView) v.findViewById(R.id.subName);
subject.setSelected(true);
code = (TextView) v.findViewById(R.id.ccode);
attended = (TextView) v.findViewById(R.id.subAttended);
absent = (TextView) v.findViewById(R.id.subAbsent);
percent = (TextView) v.findViewById(R.id.subPercent);
updated = (TextView) v.findViewById(R.id.subUpdated);
dataString = adapterkeylist.get(position);
Log.i("Info Tag Attendance","\n"+dataString.getName());
subject.setText(dataString.getName());
code.setText("Subject Code: " + dataString.getCode());
attended.setText("Classes Attended: " + dataString.getCClass());
absent.setText("Classes Absent: " + dataString.getBunked());
percent.setText("Attendance Percentage: " + dataString.getPercent());
updated.setText("Last Updated: " + dataString.getUpdated());
return v;
}
}
片段:
public class AttendanceDetailsTabFragment extends Fragment
{
AttendanceListAdapter adapter;
JSONArray ar;
JSONObject jobj;
ArrayList<JsonKeys> keyslist;
JsonKeys jk;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.attendance_listview_layout, container, false);
keyslist = new ArrayList<JsonKeys>();
String temp = new String();
try
{
ar = new JSONArray();
ar = GlobalVars.getJSON().getJSONArray("attendanceData");
for (int i = 0; i < ar.length(); i++)
{
JSONObject jObject = ar.getJSONObject(i);
jk = new JsonKeys();
jk.setCode(jObject.getString("ccode"));
temp = jObject.getString("cname");
temp = StringEscapeUtils.unescapeHtml4(temp);// To convert HTML
// special chars
// to UniCode
// format;
jk.setName(temp);
jk.setCClass(jObject.getString("cclass"));
jk.setBunked(jObject.getString("bunked"));
jk.setPercent(jObject.getString("percent"));
jk.setUpdated(jObject.getString("updated"));
Log.i("Info Tag Attendance",
jk.getCode() + "\n" + jk.getName() + "\n" + jk.getCClass() + "\n" + jk.getBunked() + "\n" + jk.getPercent() + "\n"
+ jk.getUpdated() + "\n");
keyslist.add(jk);
}
} catch (JSONException e)
{
Log.e("Error Tag", "Could Not Get JSON Array");
} finally
{
Log.i("Info Tag Attendance", "\n" + keyslist.size());
}
ListView list = (ListView) view.findViewById(R.id.list);
// adapter.notifyDataSetChanged();
adapter = new AttendanceListAdapter(getActivity(), keyslist);
list.setAdapter(adapter);
if (container == null)
{
return null;
}
return view;
}
}
可以在http://postimg.org/image/6qs3oo3hp/找到正在发生的事情的屏幕截图
如您所见,只有最后一个元素被输入到 ListView 中。欢迎任何修复。我提前为代码长度道歉。