How to load data into recyclerView Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to load data into recyclerView without wasting too much if your time.

The question is published on by Tutorial Guruji team.

In my application i want get some data from server and show into recyclerView. For application architecture i used MVP
I wrote below codes, but after loaded data from server, not show any data into recyclerView!

I used debug mode and show me data in this break point

public void add(List<DataItem> list) {
    list.addAll(list);
    notifyDataSetChanged();
}

but not show me data into recyclerView!

My Activity codes :

public class ListFragment extends Fragment implements ActiveTestsContract.View {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_tester_active_tests, container, false);
        //Initialize
        init();
        //User token log
        if (!App.isEmptyString(App.getPrefs("JWT")) || !App.getPrefs("JWT").equals(ConstKeys.EMPTY)) {
            userToken = App.getPrefs("JWT");
        }
        //Load data
        getData();

        return view;
    }

    @Override
    public void updateTestsList(Data data, int page) {
        testerDashboard_emptyLay.setVisibility(View.GONE);
        activeTests_pullToLoader.setVisibility(View.VISIBLE);
        //Get lasted page
        if (page == data.getLastPage()) {
            isHasLoadedAll = true;
            activeTests_pullToLoader.setComplete();
        }
        adapter.add(data.getData());
        //Complete items
        isLoading = false;
        nextPage = page + 1;
        activeTests_pullToLoader.setComplete();
    }

    @Override
    public void init() {
        context = getActivity();
        handler = new Handler(Looper.getMainLooper());
        testsPresenter = new ActiveTestsPresenter(this, 3);
        testerDashboard_loader = view.findViewById(R.id.testerDashboard_loader);
        activeTests_pullToLoader = view.findViewById(R.id.activeTests_pullToLoader);
        testerDashboard_emptyLay = view.findViewById(R.id.testerDashboard_emptyLay);
        emptyLayout_editProfileBtn = view.findViewById(R.id.emptyLayout_editProfileBtn);
        layoutManager = new LinearLayoutManager(context);
        recyclerView = activeTests_pullToLoader.getRecyclerView();
        //Adapter
        adapter = new TesterActiveRecyclerAdapter(activeModel, context);
        //Init recycler and adapter
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        activeTests_pullToLoader.setColorSchemeResources(R.color.colorPrimary);
    }

    private void getData() {
        activeTests_pullToLoader.isLoadMoreEnabled(true);
        activeTests_pullToLoader.setPullCallback(new PullCallback() {
            @Override
            public void onLoadMore() {
                isLoading = true;
                //Call api
                testsPresenter.testsListResponse(App.recipesApi, userToken, nextPage);
            }

            @Override
            public void onRefresh() {
                adapter.clear();
                isHasLoadedAll = false;
                isLoading = true;
                //Call api
                testsPresenter.testsListResponse(App.recipesApi, userToken, 1);
            }

            @Override
            public boolean isLoading() {
                return isLoading;
            }

            @Override
            public boolean hasLoadedAllItems() {
                return isHasLoadedAll;
            }
        });
        activeTests_pullToLoader.initLoad();
    }

Adapter codes:

public class TesterActiveRecyclerAdapter extends RecyclerView.Adapter<TesterActiveRecyclerAdapter.ViewHolder> {

    private List<DataItem> list;
    private Context context;

    public TesterActiveRecyclerAdapter(List<DataItem> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_tester_test_list, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DataItem item = list.get(position);
        //Name
        holder.txt_name.setText(item.getTitle());
        //Conditions
        if (!App.isEmptyString(item.getOs()) && !App.isEmptyString(item.getType()) && !App.isEmptyString(item.getBrowser())) {
            holder.txt_condition.setText(item.getType() + " | " + item.getOs() + " | " + item.getBrowser());
        }
        //Button actions
        holder.setState(item.getState(), position);
        //Price
        holder.rowTests_priceTxt.setText(item.getPrice() + " Dollar");
        //Animate items
        Animation animation = AnimationUtils.loadAnimation(App.context,
                (position > list.size() - 1) ? R.anim.down_from_top : R.anim.up_from_bottom);
        holder.itemView.startAnimation(animation);

        Toast.makeText(context, ""+list.get(0).getId(), Toast.LENGTH_SHORT).show();
    }

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

    public void add(List<DataItem> list) {
        list.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ViewGroup root;
        private TextView txt_name, txt_value, txt_condition, rowTests_priceTxt;
        private RoundTextView rowTests_button;

        ViewHolder(View view) {
            super(view);
            root = (ViewGroup) view;
            txt_name = view.findViewById(R.id.txtTestListTitle);
            txt_value = view.findViewById(R.id.txtTestListSublist);
            txt_condition = view.findViewById(R.id.txtTestListSublist2);
            rowTests_priceTxt = view.findViewById(R.id.rowTests_priceTxt);
            rowTests_button = view.findViewById(R.id.rowTests_button);
        }
}

How can i fix it?

Answer

You are adding data again in the same list passed in the parameter. Try to replace the below code

public void add(List<DataItem> list) {
    list.addAll(list);
    notifyDataSetChanged();
}

with

public void add(List<DataItem> list) {
    this.list.addAll(list);
    notifyDataSetChanged();
}
We are here to answer your question about How to load data into recyclerView - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji