每当我带来更多数据时重复一些项目吗?

时间:2019-01-07 21:31:00

标签: java android

我已经创建了一个加载函数来通过一个PHP文件获取项目,每次调用php文件时,该文件都会将数据库中名称的3个元素添加到Android,所以我做到了加载函数的功能在Android上显示了3个名称屏幕上出现Loadmore函数,并发现重复的问题。中间的名称与数据库不相同,请参见以下屏幕截图:https://ibb.co/sFg6p7N希望对您有所帮助并添加适当的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_customer, container, false);
    //View rootView = inflater.inflate(R.xml.pref, container, false);
    //Intent intent = new Intent(PreferenceDemoActivity.this,PrefsActivity.class);
    // startActivity(intent);
    this.context = getActivity();
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    customers = new ArrayList<>();
    adapter = new CustomerAdapter(context,customers);
    adapter.setLoadMoreListener(new CustomerAdapter.OnLoadMoreListener(){
        @Override
        public void onLoadMore() {
            recyclerView.post(new Runnable() {
                @Override
                public void run() {
                    int index = customers.size()-1;
                    loadMore(index);
                }
            });
            //Calling loadMore function in Runnable to fix the
            // java.lang.IllegalStateException: Cannot call this method 
    while RecyclerView is computing a layout or scrolling error
        }
    });
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(adapter);
    api = ServiceGenerator.createService(API.class);
    load(0);
  return rootView;
}




    private void load(int index){
      Call<List<Customer>> call = api.getCustomer(index);

       call.enqueue(new Callback<List<Customer>>(){
        @Override
        public void onResponse(Call<List<Customer>> call, final 
             Response<List<Customer>> response){
               Log.i("TRUE_TRUE_","Yes "+response.body().get(2).name);
             if(response.isSuccessful()){
               getActivity().runOnUiThread(new Runnable(){
                 public void run(){
                     customers.addAll(response.body());
                     adapter.notifyDataChanged();
                     }});
                    getActivity().runOnUiThread(new Runnable(){
                    public void run() {

                    }});

            }else{

         Log.e(TAG," Response Error "+String.valueOf(response.code()));
            }
        }
        @Override
        public void onFailure(Call<List<Customer>> call, Throwable t) {
            Log.e(TAG," Response Error "+t.getMessage());
        }
    });
}
private void loadMore(int index){
    // add loading progress view ....
    customers.add(new Customer("load"));
    adapter.notifyItemInserted(customers.size()-1);
    Log.i("customers.size() ","Yes "+customers.size()); // = 4 -1 =3
    Log.i("Index_","Yes "+index);// = 2
    Call<List<Customer>>call = api.getCustomer(index);
    call.enqueue(new Callback<List<Customer>>(){
        @Override
        public void onResponse(Call<List<Customer>> call, 
          Response<List<Customer>>response) {
            if(response.isSuccessful()){
                // remove loading view .......
                customers.remove(customers.size()-1);
               // Log.i("LastItemRemove","Yes "+response.body().get(2).name);// = 2
                List<Customer>result=response.body();
                if(result.size()>0){
                    // add loaded data
                   customers.addAll(result);
                }else{//result size 0 means there is no more data available at server
                   adapter.setMoreDataAvailable(false);
                    //telling adapter to stop calling load more as no more server data available
                    Toast.makeText(context,"No More Data Available",Toast.LENGTH_LONG).show();
                }
              adapter.notifyDataChanged();
                //should call the custom method adapter.notifyDataChanged here to get the correct loading status
            }else{
                Log.e(TAG," Load More Response Error "+String.valueOf(response.code()));
            }
        }
        @Override
        public void onFailure(Call<List<Customer>>call,Throwable t) {
            Log.e(TAG," Load More Response Error "+t.getMessage());
        }
    });
}

1 个答案:

答案 0 :(得分:0)

在您的 onLoadMore 回调中更改

int index = customers.size()-1; 

loadMore(index);

对此

loadMore(customers.size());

您正在每次加载中再次查询最后一个实体