我有一个Fragment页面,列出了从json到webservice的listview中的数据。我有一个下拉<SwipeRefreshLayout>
。我用来刷片段页面来刷新将再次调用webservice的数据并在listview中加载数据。
以下代码在刷新后不会清空旧数据。数据仍保留在列表视图中,并将刷新的数据添加到旧数据下面的列表视图中。
刷新方法我再次调用该服务
ArrayList<DataListItem> listMockData;
DataBaseAdapter HListAdapter;
swipeLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
HListAdapter.clear();
listMockData.clear();
HListAdapter.notifyDataSetChanged();
requestWebService(currentPage);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeLayout.setRefreshing(false);
}
}, 1000);
}
});
BaseAdapter
public DataBaseAdapter(Context context, ArrayList<DataListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
public void clear() {
listData.clear();
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
newsItem = listData.get(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.home_post_list, null);
holder.results = (LinearLayout) convertView.findViewById(R.id.results);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.results.setText(newsItem.getResults());
holder.position = position;
return convertView;
}
public static class ViewHolder {
TextView results;
}
编辑调用webservice
public void requestPosts(int pageId) {
JSONObject pagedata = new JSONObject();
try {
pagedata.put("pageId", pageId);
} catch (JSONException e) {
e.printStackTrace();
}
Volley.newRequestQueue(getActivity()).
add(new JsonObjectRequest(com.android.volley.Request.Method.POST,
url, pagedata
, new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonArray) {
if (jsonArray != null) {
try {
JSONObject allPost = new JSONObject(jsonArray.toString());
contacts = allPost.optJSONArray("GetPostListResult");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String Result = c.getString("Result");
results[x] = Result;
x++;
}
} catch (JSONException e) {
e.printStackTrace();
}
} else{
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
listMockData = new ArrayList<HomeListItem>();
DataListItem newsData = new DataListItem();
newsData.setResult(results[i]);
listMockData.add(newsData);
dataToListView();
}
});
}
public void dataToListView(){
listView = (ListView) root.findViewById(R.id.custom_list);
HListAdapter = new DataBaseAdapter(getActivity(), listMockData);
listView.setAdapter(HListAdapter);
}
答案 0 :(得分:0)
请尝试以下public void onResponse(JSONObject jsonArray)
if (jsonArray != null) {
listMockData.clear();
try {
contacts = jsonArray.optJSONArray("GetPostListResult");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String result = c.getString("Result");
if(result != null) {
DataListItem newsData = new DataListItem();
newsData.setResult(result);
listMockData.add(newsData);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else{
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
HListAdapter.notifyDataSetChanged();
}
我的猜测是你某处忘记重置数组或计数器变量或两者。 (result
和x
)
编辑:尝试仅将适配器设置一次,以进行初始化。我稍微改了一下代码。尝试仅清除分配的数据集listMockData
,而不是适配器。确保返回的JSON不包含重复数据。不需要dataToListView()
来刷新数据。只需清除listMockData
,然后填入新值,然后notifyDataSetChanged()
。
未来的另一个提示:对于java中变量名的第一个字母绝不是大写。