0

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);
        }
Naz141
  • 433
  • 1
  • 8
  • 31
  • 2
    Comment out the part where you call the service again, and see if the values are cleared. If so, your service will return new data together with old data. Post the code of your service. – AlbAtNf Feb 04 '16 at 15:18
  • I have posted the code which i call the service on refresh – Naz141 Feb 04 '16 at 15:42
  • Where is `results` declared? You do not seem to clear this array and it does not seem to be local? – AlbAtNf Feb 04 '16 at 15:56
  • `x` is also not local and never reset to 0, is that right? – AlbAtNf Feb 04 '16 at 15:57
  • `results` is set in a bean class by `setResults` . and using `getResults` i set it on a `TextView` in the `BaseAdapter` – Naz141 Feb 04 '16 at 15:58

1 Answers1

2

Try following code for 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();
}

My guess is that you somewhere forgot to reset an Array or a counter variable or both. (result and x)

EDIT: Try to set the adapter only once, for initialization. I changed the code a bit. Try to clear only the assigned data set listMockData, not the adapter. Make sure, the returned JSON does not contain the duplicate data. dataToListView() should not be needed to refresh data. Just clear listMockData, fill it with new values and then notifyDataSetChanged().

And another tip for the future: never upper case for the first letter for variable names in java.

AlbAtNf
  • 3,859
  • 3
  • 25
  • 29