Search code examples
androidadapternotifydatasetchanged

Android Pagination : Notifydatasetchanged() works but not how it's supposed to


I am try to implement paging in my application.The notifydatasetchanged() only loads one item rather than the whole new list of 20 elements.If someone could help me solve this? This is how I initialize my list.

public static ArrayList<HashMap<String, String>> artistsData = new ArrayList<>();

This is my post execute where I set the pagination and notify data set changed, showing a more button for this:

protected void onPostExecute(ArrayList<HashMap<String, String>> list) {
    super.onPostExecute(list);

    if (pd.isShowing()) {
        pd.dismiss();
    }
    if (list != null) {
        if (ActiveProductsFragment.page == 1 ) {
            mMostViewedAdapter = new ActiveListAdapter(context, list);
            listView.setAdapter(mMostViewedAdapter);
            Utils.b = new Button(context);

            Utils.b.setText(context.getString(R.string.more));
            Utils.b.setTextColor(000000);
            Utils.b.setTextColor(context.getResources().getColor(android.R.color.white));
            Utils.b.setBackgroundColor(context.getResources().getColor(R.color.text_color));

            Utils.b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ActiveProductsFragment.page++;

                    ActiveProductsTask artistTask = new ActiveProductsTask(context, listView);
                    artistTask.execute();
                }
            });


            listView.addFooterView(Utils.b);
        } else {


            mMostViewedAdapter = new ActiveListAdapter(context, list);
            mMostViewedAdapter.notifyDataSetChanged();
        }
    } else {
        Utils.showToastMessage(context, context.getString(R.string.no_record));
    }


}

And this is the main code of my adapter.

 public ActiveListAdapter(Context context, ArrayList<HashMap<String, String>> data) {
    this.context = context;

    this.data = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

 @Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

Solution

  • This is what solved it for me

    1-Declaring the Arraylist globally

    public static ArrayList<HashMap<String, String>> artistsData= new ArrayList<>();
    

    then doing this on post execute

    if (list != null) {
            mMostViewedAdapter = new ProductShopMallAdapter(context, list);
          if (currentPage == 1) {
                listView.setAdapter(mMostViewedAdapter);
             } else {
                 mMostViewedAdapter.notifyDataSetChanged();
            }
     }