Question

List adapter is created after the parsing of json file. Content is ok, but in the list there are only identical elements (last element). What could be wrong?

MainActivity:

CatalogAdapter catAdapter;
catAdapter = new CatalogAdapter(this, events);
setListAdapter(catAdapter);

Catalog Adapter:

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;
}
Was it helpful?

Solution

According to your code in here, try this.

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());
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top