2017-03-02 1 views
0

Je suis d'essayer d'implémenter la pagination dans mon application.The notifydatasetchanged() charge seulement un élément plutôt que la nouvelle liste entière de 20 éléments.Si quelqu'un pourrait m'aider à résoudre ce? C'est ainsi que j'initialise ma liste.Android Pagination: Notifydatasetchanged() fonctionne mais pas comment il est censé

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

Ceci est mon post exécution où je mets la pagination et notifier ensemble de données a changé, montrant un bouton plus pour cela:

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)); 
    } 


} 

Et voici le code principal de mon adaptateur.

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; 
} 

Répondre

0

C'est ce qui a résolu le problème pour moi

1-Déclaration de l'Arraylist globalement

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

faire alors ce sur le poteau exécuter

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

Dont créer une nouvelle instance d'utilisation de l'adaptateur même comme celui-ci ci-dessous

 artistsData.clear(); 
     artistsData.addAll(list); 
     mMostViewedAdapter.notifyDataSetChanged(); 

Pour une partie d'autre faire cela au lieu de

 mMostViewedAdapter = new ActiveListAdapter(context, list); 
     mMostViewedAdapter.notifyDataSetChanged(); 
+0

à mMostViewedAdapter Obtenir une exception de pointeur nul. –

+0

si c'est le cas, utilisez le code suivant – RaghavPai

+0

mMostViewedAdapter = new ActiveListAdapter (context, list); listView.setAdapter (mMostViewedAdapter); mMostViewedAdapter.notifyDataSetChanged(); – RaghavPai