Android分页:Notifydatasetchanged()的作品,但不是它应该如何

问题描述:

我尝试在我的应用程序中实现分页。notifydatasetchanged()只加载一个项目,而不是20个元素的全新列表。如果有人可以帮我解决这个?这是我如何初始化我的列表。Android分页:Notifydatasetchanged()的作品,但不是它应该如何

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

这是我的岗位执行,我设置分页和通知的数据集变化,显示了这是一个更按钮:

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


} 

这是我的适配器的主代码。

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

这就是解决了这个问题对我来说

1-全局声明Arraylist

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

然后做这个岗位上执行

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

不要创建适配器使用的新实例相同像下面这样

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

对于其它部分做到这一点,而不是

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

在mMostViewedAdapter得到一个空指针异常。 –

+0

如果是这样,请使用以下代码 – RaghavPai

+0

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