Raghvender Kumar
Raghvender Kumar

Reputation: 143

How to refresh content of listview coming from server in android?

I have a listview in my app contain content coming in response from server and have a load more Button below listview. In onCreate I execute content URL it works what I want is to in onResume method when I again execute content URL List view get double with same content and Loadmore button.How can I solve this problem.

here is my code :-

class CDealDataSent extends AsyncTask<String, Void, String> {
    public CDealAppDatastorage item;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        m_ProgressView.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... urls) {
        return DealListing(urls[0]);// sending data to server...

    }

    // onPostExecute displays the results of the AsyncTask.
    @SuppressWarnings("deprecation")
    @SuppressLint("SetTextI18n")
    @Override
    protected void onPostExecute(final String result) {

        m_ProgressView.setVisibility(View.GONE);
        try {
            m_oResponseobject = new JSONObject(result);// getting response from server
            JSONArray posts = m_oResponseobject.optJSONArray("dealList");


            for (int i = 0; i < posts.length(); i++) {
                JSONObject post = posts.getJSONObject(i);
                item = new CDealAppDatastorage();
                item.setM_szHeaderText(post.getString("dealname"));
                item.setM_szsubHeaderText(post.getString("dealcode"));
                item.setM_szDealValue(post.getString("dealvalue"));
                item.setM_n_Image(m_n_FormImage[i]);
                s_oDataset.add(item);

            }

            // LoadMore button
            Button btnLoadMore = new Button(getActivity());
            btnLoadMore.setText("LOAD MORE DEALS");
            btnLoadMore.setBackgroundResource(R.drawable.button_boarder);
            btnLoadMore.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small);
            btnLoadMore.setTextColor(Color.WHITE);
            btnLoadMore.setGravity(Gravity.CENTER);
            if (!s_oDataset.isEmpty()) {
                // Adding Load More button to lisview at bottom
                m_ListView.addFooterView(btnLoadMore);
                m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
                m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
                m_oAdapter.notifyDataSetChanged();
            } else {
                btnLoadMore.setVisibility(View.GONE);
            }


            btnLoadMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    m_n_DefaultRecordCount = m_n_DefaultRecordCount + 5;// increment of record count by 5 on next load data
                    m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above

                    sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
                    sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
                    new DealNext().execute(m_DealListingURL);// POST DATA TO SERVER TO LOAD MORE DATA......
                }
            });

            getResponse();
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}

Upvotes: 1

Views: 94

Answers (2)

Haniyeh Khaksar
Haniyeh Khaksar

Reputation: 804

you have 2 solution: 1. check if the item exist in your data, don't add it.

if(!s_oDataset.contains(item)){
s_oDataset.add(item);
}
  1. remove all data before add new data.

    s_oDataset.removeAll();

Upvotes: 0

Fiil
Fiil

Reputation: 1740

Before doing:

s_oDataset.add(item);

check if it already has this value:

if(!s_oDataset.contains(item)){
    s_oDataset.add(item);
}

Upvotes: 1

Related Questions