I have a Fragment page listing data in the listview from a json through a webservice. I have a pull down <SwipeRefreshLayout>
. i use to swipe the fragment page to refresh the data which which will call the webservice again and load the data in listview.
The code below does not empty the old data after refresh. the data still remains in the listview and add the refreshed data to the listview below the old data.
to refresh method i call the service again
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;
}
Edit calling 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);
}