RecyclerView shows layout and size is correct but the setText is still null and empty

43 Views Asked by At

I am doing a vaccine name list which shows the list of vaccine names from a table in a database. Once run, the RecyclerView was multiplied to the correct array size which is 6 but the names were not showing and null.

These are my block of codes for that particular list view

AboutVaccines.java

private void viewJsonData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        myApi = retrofit.create(MyAPI.class);

        Call<ArrayList<VaccineModel>> modelarraylist = myApi.callArrayList();
        modelarraylist.enqueue(new Callback<ArrayList<VaccineModel>>() {
            @Override
            public void onResponse(Call<ArrayList<VaccineModel>> call, retrofit2.Response<ArrayList<VaccineModel>> response) {
                modelArrayList = response.body();
                int i = 0;

                for(i = 0; i < modelArrayList.size(); i++){
                    vaccineAdapter = new VaccineAdapter(modelArrayList, AboutVaccines.this);

                    LinearLayoutManager manager = new LinearLayoutManager(AboutVaccines.this,
                            RecyclerView.VERTICAL, false);
                    vaccinerecycler.setLayoutManager(manager);
                    vaccinerecycler.setAdapter(vaccineAdapter);
                }
            }

            @Override
            public void onFailure(Call<ArrayList<VaccineModel>> call, Throwable t) {
                Toast.makeText(AboutVaccines.this, "data was not fetched!", Toast.LENGTH_SHORT).show();
            }
        });

    }

VaccineAdapter.java

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

    private ArrayList<VaccineModel> modelArrayList;
    private Context context;

    public VaccineAdapter(ArrayList<VaccineModel> modelArrayList, Context context) {
        this.modelArrayList = modelArrayList;
        this.context = context;
    }

    @NonNull
    @Override
    public VaccineAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.abtvaccinesrecyclerview, parent, false);
        return new VaccineAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VaccineAdapter.ViewHolder holder, int position) {
        holder.vname.setText(modelArrayList.get(position).getVaccineName());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView vname;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            vname = itemView.findViewById(R.id.vaccinename);
        }
    }
}

MyAPI.java

public interface MyAPI {
    @GET("vaccinedata.php")
    Call<ArrayList<VaccineModel>> callArrayList();
}

RecyclerView shows 6 layouts but the vaccine names is still not shown

The logcat also shows error "No adapter attached; skipping layout" for the RecyclerView

0

There are 0 best solutions below