1

I am trying to fetch data from server and showing it in recycler view.I am using retrofit library and RxJava2 but its unable to fetch data from server.

Its showing following line in LogCat:

E/RecyclerView: No adapter attached; skipping layout

Response form the server:

[
  {
    "term_id": "4",
    "name": "Entertainment"
  },
  {
    "term_id": "5",
    "name": "Tech & Gadgets"
  },
  {
    "term_id": "6",
    "name": "Sports"
  },
  {
    "term_id": "7",
    "name": "Health and Fitness Tips"
  }
]

Below is my code:

RetrofitClient.java

public class RetrofitClient {

private static Retrofit retrofit = null;

public static Retrofit getInstance(){


    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(22,TimeUnit.SECONDS)
            .readTimeout(22, TimeUnit.SECONDS)
            .writeTimeout(22, TimeUnit.SECONDS)
            .build();

    if(retrofit == null)
        retrofit = new Retrofit.Builder()
                .baseUrl("https://www.flypped.com/api/")
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();

    return retrofit;

  }

}

ApiService.class

public interface ApiService {

@GET("Categoery_api")
Observable<List<Model>> getData();
}

Model.java

public class Model {

@SerializedName("catId")
@Expose
String catId;

@SerializedName("catName")
@Expose
String catName;

public Model(){

}

public Model(String catId, String catName) {
    this.catId = catId;
    this.catName = catName;
}

public String getCatId() {
    return catId;
}

public void setCatId(String catId) {
    this.catId = catId;
}

public String getCatName() {
    return catName;
}

public void setCatName(String catName) {
    this.catName = catName;
}

}

MainActivity.java

private void fetchData(){

    Retrofit retrofit = RetrofitClient.getInstance();
    ApiService myApi = retrofit.create(ApiService.class);

     myApi.getData().subscribeOn(Schedulers.io())
                                           .observeOn(AndroidSchedulers.mainThread())
                                           .subscribe(new Observer<List<Model>>() {
                                               @Override
                                               public void onSubscribe(Disposable d) {
                                                   d.dispose();
                                               }

                                               @Override
                                               public void onNext(List<Model> models) {

                                                   if(models.size() > 0){

                                                       progress.setVisibility(View.INVISIBLE);

                                                      adapter = new PostAdapter(getApplicationContext(),list);
                                                       recycler.setAdapter(adapter);
                                                   }
                                               }

                                               @Override
                                               public void onError(Throwable e) {

                                                   Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                                               }

                                               @Override
                                               public void onComplete() {


                                               }
                                           });

                           }

PostAdapter.java

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

List<Model> list = new ArrayList<>();
Context context;

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

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

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_row,parent,false);

    ViewHolder view = new ViewHolder(v);
    return view;
}

@Override
public void onBindViewHolder(@NonNull PostAdapter.ViewHolder holder, int position) {

    Model model = list.get(position);

    holder.catName.setText(model.getCatName());
    holder.catId.setText(model.getCatId());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView catId,catName,comp,titl;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        catId = itemView.findViewById(R.id.catId);
        catName = itemView.findViewById(R.id.catName);
    }
  }
}

Someone please let me know what I am doing wrong any help would be appreciated.

THANKS

Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

1

You are calling d.dispose(); in onSubscribe which will dispose the resource and result so remove dispose call as

public void onSubscribe(Disposable d) {
    //d.dispose(); remove it
}

you can move dispose in onDestroy to free up resources(JIC request is still running) when activity(or fragment) is going to be removed from memory and make sure you have layout manager set on recycler view.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • https://stackoverflow.com/questions/60356951/how-can-you-close-a-question-without-reading-it – Lena Bru Feb 22 '20 at 21:28
  • I have removed `d.dispose()` now it is showing total count of json objects in log cat but not showing data in recycler view. – Digvijay Feb 23 '20 at 04:49
  • @Digvijay have you added the layout manager `recycler.setLayoutManager(new LinearLayoutManager(context)); recycler.setAdapter(adapter);` if yes then make sure the layouts are setup correctly – Pavneet_Singh Feb 23 '20 at 11:17
  • Problem has been solved actually the problem was in Model class serializable key was not matching with server keys. – Digvijay Feb 23 '20 at 12:58