RecycleViewer doesn't have adapter attached. Also Error on URL

Issue

I am creating an app in which I want to call an API using retrofit. And displaying the objects in a Recycle Viewer. I get error while I try to use my method which calls the API and puts the objects in the Recycle viewer.

Below is the Code for the Pager Adapter.

public class IssueAdapter2 extends RecyclerView.Adapter<IssueAdapter2.SymptomViewHolder> {
    private ArrayList<Issue> dataList;

public IssueAdapter2(ArrayList<Issue> dataList){
    this.dataList = dataList;
}

@NonNull
@Override
public SymptomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
    View view = layoutInflater.inflate(R.layout.row_item, viewGroup, false);
    return new SymptomViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SymptomViewHolder symptomViewHolder, int position) {
    symptomViewHolder.symptomTitle.setText(dataList.get(position).getName());

}

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

public class SymptomViewHolder extends RecyclerView.ViewHolder {

    TextView symptomTitle;

    public SymptomViewHolder(@NonNull View itemView) {
        super(itemView);
        symptomTitle = itemView.findViewById(R.id.IssueName);
    }
}

And the code where I call the retrofit and the method that creates the recycle view:

public class MainActivity extends AppCompatActivity {

    private Button btnDiagnosis;
    private RadioButton radioButton;
    public TextView textResults;
private RecyclerView recyclerView;
private IssueAdapter2 issueAdapter2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_layout);

    btnDiagnosis = findViewById(R.id.bDiagnosis);

    radioButton = findViewById(R.id.feverYes);

    btnDiagnosis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startButtonActivity();
        }
    });


}

   public void startButtonActivity(){
        //create Retrofit instance
       setContentView(R.layout.results);
       //textResults = findViewById(R.id.resultsview);

       Retrofit.Builder builder = new Retrofit.Builder()
               .baseUrl("https://priaid-symptom-checker-v1.p.mashape.com/diagnosis/")
               .addConverterFactory(GsonConverterFactory.create());

       Retrofit retrofit = builder.build();

       //get Symptoms


       JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
       Call<ArrayList<Issue>> call = jsonPlaceHolderApi.searchForIssues(

               "male",
               "10,5",
               "en-gb",
               1994);

       Log.wtf("URL Called", call.request().url() + "");
       call.enqueue(new Callback<ArrayList<Issue>>() {
           @Override
           public void onResponse(Call<ArrayList<Issue>> call, Response<ArrayList<Issue>> response) {

               if(response.isSuccessful()){
                   configViews(response.body());
               }
           }
           @Override
           public void onFailure(Call<ArrayList<Issue>> call, Throwable t) {
               Toast.makeText(MainActivity.this, "Something went wrong...Error message: " + t.getMessage(),
                       Toast.LENGTH_LONG).show();
           }
       });
   }

    private void configViews(ArrayList<Issue> issueArrayList) {



        recyclerView = findViewById(R.id.recyclerview);

        issueAdapter2 = new IssueAdapter2(issueArrayList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(issueAdapter2);

    }

}

The error that I am getting:

E/URLĀ Called: https://priaid-symptom-checker-v1.p.mashape.com/diagnosis?Gender=male&Symptoms=10,5&Language=en-gb&Year%20of%20Birth=1994

E/RecyclerView: No adapter attached; skipping layout

Please if anyone can tell me how to overcome this and how my recycleview will get the adapter

Thank you so much!

Solution

Most propably callback from call.enqueue(…) is not called on a main thread. On Android it is the only place where UI can be updated. Setting adapter on main thread should solve your issue:

 private void configViews(ArrayList<Issue> issueArrayList) {
    recyclerView = findViewById(R.id.recyclerview);
    issueAdapter2 = new IssueAdapter2(issueArrayList);
    recyclerView.setHasFixedSize(true);
    recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(issueAdapter2);
        }
    );
}

Answered By – Igor 101

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published