Android 新数据替换ArrayList中的旧数据

Android 新数据替换ArrayList中的旧数据,android,arraylist,Android,Arraylist,我有一个应用程序,我需要在列表视图中显示数据。为此,我使用自定义适配器。为了存储我的信息,我用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

我有一个应用程序,我需要在列表视图中显示数据。为此,我使用自定义适配器。为了存储我的信息,我用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 {

    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 ();
            }
        }
    }
受保护的void onPostExecute(字符串s){
values=newarraylist();
数据=新的搜索模型();
super.onPostExecute(s);
如果(!s.trim().包含(“表”)){
Crouton.makeText((android.app.Activity)c,“找不到任何东西”,Style.INFO.show();
}否则{
试一试{
JSONObject JSONObject=新的JSONObject;
JSONObject NewDataSet=JSONObject.getJSONObject(“NewDataSet”);
if(NewDataSet.get(“表”)JSONObject实例){
JSONObject table=NewDataSet.getJSONObject(“table”);
data.setAddress(table.getString(“地址”);
data.setCounts(table.getString(“候选计数”);
values.add(数据);
}else if(NewDataSet.get(“Table”)JSONArray实例){
JSONArray tableArray=NewDataSet.getJSONArray(“表格”);
for(inti=0;i
问题似乎在于,每次通过
for
循环,您都没有创建
SearchModel
的新实例,因此
values.add(data);
只是向同一个
SearchModel
实例添加了另一个引用。也就是说,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);
            }
        }
        ...
        ...
受保护的void onPostExecute(字符串s){
values=newarraylist();

//数据=新的搜索模型();先生,你能帮我创建一个模型吗?我没有地址,也没有很多字段,所以我怎么能构建一个模型呢?如果你有很多数据字段,那么你现在的做法可能就是这样。只要在需要它们之前不要创建实例。也就是说,不要调用
data=new SearchModel()
直到调用
setAddress()
setCounts()
等之前。是的,在这种情况下,忘记构造函数。你的方法很好。只需移动
数据=新的SearchModel();
添加到for循环的开头,并将其添加到if块的
JSONObject
部分。给我一分钟,我会编辑我的答案。我只是想与你保持联系,以便向你学习。如果你愿意,我可以发布我的电子邮件id,然后在那里回复我?