Anuj
Anuj

Reputation: 129

New data replaces old data in ArrayList

I have an app in which i need to show data in List view.For that i am using custom Adapter.To store my info i have created Model class with getter and setter.The info which i am getting is in Json Format.So i am parsing it and storing it in my Model Class.I have user ArrayList to store info.But my prob is when parsing data from Json array the new data replaces the old data in the ArrayList. For example

loop 1
data=1,2,5
ArrayList at [0]=1,2,5

loop 2
data=2,2,2
ArrayList at [0]=2,2,2
             [1]=2,2,2

Now why this is happening??

Model Class

public class SearchModel {

public class SearchModel {

    private String category, minExp, maxExp, postedOn, counts, applied, position, desc, type, hour, status, expiryDate, address, gender, religion, summary, requestId, requestorId;

    public SearchModel(String category, String minExp, String maxExp, String postedOn, String counts, String applied, String position, String desc, String type, String hour, String status, String expiryDate, String address, String gender, String religion, String summary, String requestId, String requestorId) {
        this.category = category;
        this.minExp = minExp;
        this.maxExp = maxExp;
        this.postedOn = postedOn;
        this.counts = counts;
        this.applied = applied;
        this.position = position;
        this.desc = desc;
        this.type = type;
        this.hour = hour;
        this.status = status;
        this.expiryDate = expiryDate;
        this.address = address;
        this.gender = gender;
        this.religion = religion;
        this.summary=summary;
        this.requestId = requestId;
        this.requestorId = requestorId;
    }
}

Async Class

 protected void onPostExecute(String s) {
        values = new ArrayList<SearchModel> ();
        data = new SearchModel ();

        super.onPostExecute (s);
        if (!s.trim ().contains ("Table")) {
            Crouton.makeText ((android.app.Activity) c, "Nothing found", Style.INFO).show ();
        } else {
            try {

                JSONObject jsonObject = new JSONObject (s);
                JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                if (NewDataSet.get ("Table") instanceof JSONObject) {
                    JSONObject table = NewDataSet.getJSONObject ("Table");
                    data.setAddress (table.getString ("Address"));
                    data.setCounts (table.getString ("Candidate_Counts"));
                    values.add (data);

                } else if (NewDataSet.get ("Table") instanceof JSONArray) {
                    JSONArray tableArray = NewDataSet.getJSONArray ("Table");
                    for (int i = 0; i < tableArray.length (); i++) {
                        JSONObject table = tableArray.getJSONObject (i);
                        data.setAddress (table.getString ("Address"));
                        data.setCounts (table.getString ("Candidate_Counts"));
                        values.add (data);

                    }

                }


            } catch (JSONException e) {
                e.printStackTrace ();
            }
        }
    }

Upvotes: 0

Views: 186

Answers (1)

Mike M.
Mike M.

Reputation: 39191

It seems the problem is that you're not creating a new instance of SearchModel each time through the for loop, so values.add (data); is just adding another reference to the same SearchModel instance. That is, every item in your ArrayList is pointing to one object. Try changing your code as follows:

protected void onPostExecute(String s) {
    values = new ArrayList<SearchModel> ();
    // data = new SearchModel ();   << Remove this

    // super.onPostExecute (s);  << You don't need this, by the way.
    if (!s.trim ().contains ("Table")) {
    ...
    ...
        if (NewDataSet.get ("Table") instanceof JSONObject) {
            JSONObject table = NewDataSet.getJSONObject ("Table");

            data = new SearchModel ();  //  << Add this
            data.setAddress (table.getString ("Address"));
            data.setCounts (table.getString ("Candidate_Counts"));
            values.add (data);
        } else if (NewDataSet.get ("Table") instanceof JSONArray) {
            JSONArray tableArray = NewDataSet.getJSONArray ("Table");
            for (int i = 0; i < tableArray.length (); i++) {
                JSONObject table = tableArray.getJSONObject (i);

                data = new SearchModel ();  //  << Add this
                data.setAddress (table.getString ("Address"));
                data.setCounts (table.getString ("Candidate_Counts"));
                values.add (data);
            }
        }
        ...
        ...

Upvotes: 1

Related Questions