0
我有一個應用程序,我需要在列表視圖中顯示數據。爲此我使用自定義Adapter.To存儲我的信息我已創建模型類與getter和setter.The信息我得到的是Json格式。所以我解析它並將其存儲在我的模型類中。我有用戶ArrayList來存儲info.But我的概率是從Json數組解析數據新數據替換ArrayList中的舊數據。 例如新數據替換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
現在爲什麼會這樣?
模型類
公共類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();
}
}
}
先生,請你能幫我如何創建模型? Ans我只有地址和計數bt很多字段,所以我如何構建一個模型 – Anuj
好吧,如果你有一個數據字段的_lot_,那麼你現在做的方式可能是要走的路。只是在你需要它們之前不要創建實例。也就是說,不要調用'data = new SearchModel();'直到調用'setAddress()','setCounts()'之前等。 –
見上面我編輯過我的模型。 這是正確的方法嗎? – Anuj