मुझे अपने आवेदन में कोई समस्या थी... मुझे नहीं पता कि मेरे RecyclerView adapter को सूचित क्यों नहीं किया गया, मुझे पता है कि मेरी सूची अपडेट हो रही है और मेरी समस्या मेरे RecyclerView के साथ है जो अद्यतन, क्या कोई मेरी मदद कर सकता है?

मुख्य प्रस्तुतकर्ता.जावा:

public class MainPresenter implements MainContract.MainPresenter {

private MainContract.MainView mView;
private Context context;
private boolean firstTime  = true;
private static List<PlayerModel> playerRecyclerList = new ArrayList<>();
private RetrofitAdapter retrofitAdapter;
private DaoSession daoSession;
private boolean loadMore = true;


MainPresenter(MainActivity mainActivity,DaoSession daoSession){
    this.daoSession = daoSession;
    this.mView = mainActivity;
    this.context = mainActivity;
}

@Override
public void fabClicked() {
    Intent intent = new Intent(context, EditActivity.class);
    intent.putExtra("create",true);
    context.startActivity(intent);
}

@Override
public void itemClicked(int pos) {
    PlayerModel playerModel = retrofitAdapter.getPlayerRecyclerList().get(pos);
    Intent intent = new Intent(context,DetailActivity.class);
    intent.putExtra(Repository.NAME,playerModel.getName());
    intent.putExtra(Repository.CITY,playerModel.getCity());
    intent.putExtra(Repository.COUNTRY,playerModel.getCountry());
    intent.putExtra(Repository.IMAGE_URL,playerModel.getImgUrl());
    context.startActivity(intent);
}

@Override
public void loadData() {
    if(!firstTime) {

        playerRecyclerList.clear();
        retrofitAdapter.notifyDataSetChanged();
    }
    loadDatabase();
    firstTime = false;
}

private void loadDatabase() {
    PlayerDao playerDao = daoSession.getPlayerDao();
    List players = playerDao.queryBuilder().list();
    Object[] playerArray = players.toArray();
    for(Object player : playerArray){
        ModelRecyclerHandler((Player) player);
    }
    retrofitAdapter = new RetrofitAdapter(context,this);
    retrofitAdapter.setList(playerRecyclerList,false);
    mView.loadRecycler(retrofitAdapter);
}

private void ModelRecyclerHandler(Player player) {
    PlayerModel playerModel = new PlayerModel();
    String name = player.getName();
    String city = player.getCity();
    String country = player.getCountry();
    String imgUrl = player.getImgUrl();
    Long ID = player.getId();
    playerModel.setName(name);
    playerModel.setCity(city);
    playerModel.setCountry(country);
    playerModel.setImgUrl(imgUrl);
    playerModel.setID(ID);
    playerRecyclerList.add(playerModel);
}

public void update(){
    playerRecyclerList.clear();
    PlayerDao playerDao = daoSession.getPlayerDao();
    playerDao.deleteAll();
    fetchPlayerList();
}

private void fetchPlayerList() {

    RecyclerInterface api = RetrofitApi.getInstance().create(RecyclerInterface.class);

    Call<String> call = api.getString();
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
            assert response.body() != null;
            if (response.isSuccessful()) {
                mView.onSuccessGetApi();
                String jsonresponse = response.body();
                writeDatabase(jsonresponse);
            }
        }

        @Override
        public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {

        }
    });
}

private void writeDatabase(String response) {
    String name,country,city,imgUrl;
    try {
        JSONObject obj = new JSONObject(response);
        if(obj.optString("status").equals("true")){
            JSONArray dataArray = obj.optJSONArray("data");
            assert dataArray != null;
            for(int i = 0; i<dataArray.length(); i++)
            {
                JSONObject dataobj = dataArray.getJSONObject(i);
                PlayerModel playerModel = new PlayerModel();
                name = dataobj.getString("name");
                city = dataobj.getString("city");
                country = dataobj.getString("country");
                imgUrl = dataobj.getString("imgURL");
                insertPlayer(name,country,city,imgUrl, (long) i);
                playerModel.setName(name);
                playerModel.setCity(city);
                playerModel.setCountry(country);
                playerModel.setImgUrl(imgUrl);
                playerModel.setID((long) i);
                playerRecyclerList.add(playerModel);
            }
            retrofitAdapter = new RetrofitAdapter(context,this);
            retrofitAdapter.setList(playerRecyclerList,false);
            mView.loadRecycler(retrofitAdapter);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void insertPlayer(String name,String city,String country,String imgUrl,Long ID)
{
    PlayerDao playerDao = daoSession.getPlayerDao();
    Player player = new Player();
    player.setName(name);
    player.setCity(city);
    player.setCountry(country);
    player.setImgUrl(imgUrl);
    player.setId(ID);
    playerDao.insert(player);
    player = null;
}

public void editPlayer(int pos){
    PlayerModel playerModel = getPlayerRecyclerList().get(pos);
    Intent intent = new Intent(context,EditActivity.class);
    intent.putExtra(Repository.NAME,playerModel.getName());
    intent.putExtra(Repository.CITY,playerModel.getCity());
    intent.putExtra(Repository.COUNTRY,playerModel.getCountry());
    intent.putExtra(Repository.IMAGE_URL,playerModel.getImgUrl());
    intent.putExtra("ID",playerModel.getID());
    context.startActivity(intent);
}

public void deleteFromDB(int pos){
    PlayerModel playerModel;
    playerModel = playerRecyclerList.get(pos);
    PlayerDao playerDao = daoSession.getPlayerDao();
    playerDao.deleteByKey(playerModel.getID());
    playerRecyclerList.clear();
    retrofitAdapter.notifyItemChanged(pos);
    loadDatabase();
}

@Override
public void onLongClick(int pos) {

    mView.longClick(pos);
}

@Override
public void onBind(RetrofitAdapter.MyViewHolder holder, int position) {
    PlayerModel playerModel = playerRecyclerList.get(position);
    Glide.with(context).load(playerModel.getImgUrl()).into(holder.iv);
    holder.name.setText(playerModel.getName());
    holder.country.setText(playerModel.getCountry());
    holder.city.setText(playerModel.getCity());
}

public void scroll(RecyclerView recyclerView){
    if(!recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN)) {
        if (loadMore) {
            loadMoreDb();
        }
    }
}

private void loadMoreDb() {
    playerRecyclerList.clear();
    loadMore = false;
    mView.showProgressBar();
    android.os.Handler handler = new Handler();
    handler.postDelayed(() -> mView.hideProgressBar(), 1000);
    PlayerDao playerDao = daoSession.getPlayerDao();
    List players = playerDao.queryBuilder().list();
    Object[] playerArray = players.toArray();
    for (Object player : playerArray) {
        ModelRecyclerHandler((Player) player);
    }
    retrofitAdapter.setList(playerModelRecyclerList,true);
    loadMore = true;
}

private List<PlayerModel> getPlayerRecyclerList() {return playerRecyclerList; }

@Override
public int getItemCount() {
    return playerRecyclerList.size();
}

public RetrofitAdapter getRetrofitAdapter(){return retrofitAdapter;}

और ये रहा मेरा Retrofit adapter रेट्रोफिट एडेप्टर.जावा:

public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.MyViewHolder> {

    private List<PlayerModel> playerRecyclerList;
    private Context context;
    private MainContract.MainPresenter presenter;

    RetrofitAdapter(Context ctx, MainContract.MainPresenter presenter) {
        this.context = ctx;
        this.playerRecyclerList = new ArrayList<>();
        this.presenter = presenter;
    }


    void setList(List<PlayerModel> list, boolean loadMore) {

        if (loadMore) {
            int size = playerRecyclerList.size();
            for (PlayerModel m : list) {
                playerRecyclerList.add(m);
            }
            this.notifyItemRangeChanged(size, playerRecyclerList.size());
        } else {
            playerRecyclerList.addAll(list);
        }
    }


    List<PlayerModel> getPlayerRecyclerList() {
        return playerRecyclerList;
    }


    @NonNull
    @Override
    public RetrofitAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.retro_item, parent, false);
        return new MyViewHolder(view,presenter);
    }

    @Override
    public void onBindViewHolder(@NonNull RetrofitAdapter.MyViewHolder holder, int position) {
        presenter.onBind(holder, position);
    }

    @Override
    public int getItemCount() {return presenter.getItemCount(); }


    static class MyViewHolder extends RecyclerView.ViewHolder {


        @InjectView(R.id.name)
        TextView name;
        @InjectView(R.id.city)
        TextView city;
        @InjectView(R.id.iv)
        ImageView iv;
        @InjectView(R.id.country)
        TextView country;

        MyViewHolder(View itemView, MainContract.MainPresenter presenter) {
            super(itemView);
            ButterKnife.inject(this, itemView);

            itemView.setOnClickListener(v -> presenter.itemClicked(getAdapterPosition()));


            itemView.setOnLongClickListener(v -> {

                presenter.onLongClick(getAdapterPosition());
                return false;
            });

        }
    }
}

और मेरा Main Activity: मुख्य गतिविधि.जावा

public class MainActivity extends AppCompatActivity implements MainContract.MainView{

    MainContract.MainPresenter presenter;
    @InjectView(R.id.recycler)
    RecyclerView recyclerView;
    @InjectView(R.id.rl)
    SwipeRefreshLayout refreshLayout;
    @InjectView(R.id.progressBar)
    ProgressBar progressBar;
    static RetrofitAdapter retrofitAdapter;
    DaoSession daoSession;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        initView();
    }

    @Override
    public void onResume(){
        super.onResume();
        presenter.loadData();
    }

    private void initView() {
        daoSession = ((AppController) getApplication()).getDaoSession();
        presenter = new MainPresenter(this,daoSession);
        refreshLayout.setOnRefreshListener(this::refresh);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                presenter.scroll(recyclerView);
                //retrofitAdapter = presenter.getRetrofitAdapter();
                //retrofitAdapter.notifyDataSetChanged();
            }
        });
    }

    private void refresh() {
        final Handler handler = new Handler();
        handler.postDelayed(() -> {
            presenter.update();
            if(refreshLayout.isRefreshing()){
                refreshLayout.setRefreshing(false);
            }
        },1000);
    }

    @Override
    public void loadRecycler(RetrofitAdapter retrofitAdapter) {
        MainActivity.retrofitAdapter = retrofitAdapter;
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(MainActivity.retrofitAdapter);
    }


    @Override
    public void longClick(int position) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        String[] options = new String[2];
        options[0] = "Edit ";
        options[1] = "Delete ";
        alertDialogBuilder.setItems(options, (dialog, which) -> {
            if(which == 0){
                presenter.editPlayer(position);
            }else if(which == 1){
                presenter.deleteFromDB(position);
            }
        });
        alertDialogBuilder.create().show();

    }

    @Override
    public void showProgressBar() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        progressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onSuccessGetApi() {

        if(refreshLayout.isRefreshing()){
            refreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void addData(List<PlayerModel> players) {
        retrofitAdapter.setList(players,true);
        retrofitAdapter.notifyDataSetChanged();
        recyclerView.setAdapter(retrofitAdapter);
    }


    @OnClick(R.id.fab)
    public void fabClick(){
        presenter.fabClicked();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ButterKnife.reset(this);
    }
}

मैं किसी भी विचार की सराहना करता हूं!
धन्यवाद।

0
Tyr 29 फरवरी 2020, 09:00
आरंभिक या पृष्ठ पर अंक लगाना से प्रदर्शित नहीं हो रहा है?
 – 
Rajasekaran M
29 फरवरी 2020, 09:16
जब मैं ऐप खोलता हूं तो यह सही तरीके से काम करता है लेकिन अगर मैं नीचे स्क्रॉल करता हूं तो यह रीसाइक्लर व्यू को अपडेट नहीं करेगा और इसके लिए यह अधिक स्क्रॉल नहीं करेगा और रीसाइक्लर व्यू पुरानी सामग्री दिखाता है
 – 
Tyr
29 फरवरी 2020, 09:20
मेरा उत्तर जांचें
 – 
Rajasekaran M
29 फरवरी 2020, 09:36

1 उत्तर

आपके एडॉप्टर को ठीक से सूचित नहीं किया गया है इसलिए आपको अपना setList() अपडेट करना होगा

void setList(List<PlayerModel> list, boolean loadMore) {

    if (loadMore) {
        int size = playerRecyclerList.size();
        for (PlayerModel m : list) {
            playerRecyclerList.add(m);
        }

        this.notifyItemRangeChanged(this.playerRecyclerList.size - size, this.playerRecyclerList.size)
    } else {
        playerRecyclerList.addAll(list);
    }
}

मुझे आशा है कि यह मददगार होगा :-)

0
Rajasekaran M 29 फरवरी 2020, 09:35
आपकी मदद के लिए धन्यवाद लेकिन यह काम नहीं किया
 – 
Tyr
29 फरवरी 2020, 09:51