新数据替换ArrayList中的旧数据

时间:2014-06-25 10:33:52

标签: android arraylist

我有一个应用程序,我需要在列表视图中显示数据。因为我使用自定义Adapter.To存储我的信息我已经创建了模型类与getter和setter。我得到的信息是在Json格式因此,我正在解析它并将其存储在我的模型类中。我有用户存储信息的ArrayList。但我的问题是,当从Json数组解析数据时,新数据将替换ArrayList中的旧数据。 例如

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

现在为什么会发生这种情况?

模型类

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

异步类

 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 ();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

似乎问题是您每次通过SearchModel循环都没有创建for的新实例,因此values.add (data);只是添加对同一SearchModel的另一个引用1}}实例。也就是说,ArrayList中的每个项都指向一个对象。尝试更改代码,如下所示:

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);
            }
        }
        ...
        ...