在解析json文件后创建列表适配器。内容没问题,但在列表中只有相同的元素(最后一个元素)。可能有什么不对?
MainActivity:
CatalogAdapter catAdapter;
catAdapter = new CatalogAdapter(this, events);
setListAdapter(catAdapter);
目录适配器:
CatalogAdapter(Context context, ArrayList<ListData> _events) {
cont = context;
events = _events;
lInflater = (LayoutInflater) cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return events.size();
}
public ListData getItem(int position) {
return events.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.inetlist, parent, false);
}
ListData p = getItem(position);
((TextView) view.findViewById(R.id.title)).setText(p.title);
((TextView) view.findViewById(R.id.decription)).setText("Описание: \n"+p.description);
return view;
}
答案 0 :(得分:1)
根据here中的代码,试试这个。
public void parseJson ()
{
events = new ArrayList<ListData>();
try {
object = new JSONObject(jsonString);
data = object.getJSONArray("events");
for ( int i = 0; i < data.length(); i++ )
{
ListData oneEvent = new ListData();
oneEvent.concertID = data.getJSONObject(i).getString("id");
oneEvent.title = data.getJSONObject(i).getString("title");
oneEvent.description = data.getJSONObject(i).getString("desc");
oneEvent.pic = null;
events.add(oneEvent);
Log.e("Event", oneEvent.concertID + " " + oneEvent.description + " " + oneEvent.title);
}
}
catch (JSONException exc) {
Log.e("log_tag", "Error in parse" + exc.toString());
}
}