我设计了一个自定义BaseAdapter,它接受一个Class(JsonKeys)的ArrayList,它包含我需要的数据的getter和setter。
我基本上是在解析JSON数组并将适当的数据插入到listrows中,如下所示:
"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和Fragment的类,它们分别膨胀了listview。
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。 任何修复都是受欢迎的。 我提前解决了代码长度。
答案 0 :(得分:0)
没关系,我刚刚实现了在全局类(而不是数组)中设置变量,所以很明显只记录了最后输入的数据(Facepalm)。
愚蠢的错误,将使用HashMap重新编写代码。
答案 1 :(得分:0)
我在android中遇到了同样的问题,
我意识到我将listitemclass的变量设置为static,因此当从其适配器返回视图时,它总是引用相同的变量。
删除静态部分,它运行正常