0

解析 json 文件后创建列表适配器。内容还可以,但列表中只有相同的元素(最后一个元素)。有什么问题?

主要活动:

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;
}
4

1 回答 1

1

根据你在这里的代码,试试这个。

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());
        }
    }
于 2013-11-05T09:04:36.007 回答