Android Adapter數據的刷新和添加(刷新改變原數據,添加不改變原數據,在後面添加,類是於下拉刷新)
今天在做Adapter適配的時候,發現我原來的數據沒有刷新,而是直接在後面加載的,然後看了一下自己寫的,發現了,一個自己寫錯的方法,然後發現了這個效果,以後的下拉刷新數據就可以這樣來填充。
好了,思想+代碼。
代碼:
//數據填充
private List<ListEntity> vJsonStr(String objStr){
try {
JSONObject jsonObject=new JSONObject(objStr)
JSONArray result=jsonObject.optJSONArray("List")
if(result.length() == 0) {
return null
}
List.clear()
for(int i = 0
JSONObject newsObj = result.optJSONObject(i)
vedioListEntity = new VedioListEntity()
vedioListEntity.setID(newsObj.optInt("ID"))
vedioListEntity.setVedioName(newsObj.optString("VName"))
vedioListEntity.setAuthorName(newsObj.optString("AName"))
vedioListEntity.setAuthorID(newsObj.optInt("AID"))
vedioListEntity.setImageUrl(newsObj.optString("Image"))
vedioList.add(vedioListEntity)
}
} catch (JSONException e) {
e.printStackTrace()
}
handler.obtainMessage(12345,"").sendToTarget()
return vedioList
關鍵代碼:
List.clear();
關鍵就是這個,清除list的方法。當有這個時第二次填充,會先刪除原先的,然後再填充,如果沒有,就直接填充。