0

I am trying to fetch data from server and showing it in recycler view. I am using Retrofit library and RxJava2 but it's unable to fetch data from server.It is showing total count in LogCat but not showing data in recycler view.

Response from 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

 public class MainActivity extends AppCompatActivity { RecyclerView recycler; ProgressBar progress; List<Model> list = new ArrayList<>();; PostAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = findViewById(R.id.progress); recycler = findViewById(R.id.recycler); recycler.setHasFixedSize(true); recycler.setLayoutManager(new LinearLayoutManager(this)); fetchData(); } 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) { } @Override public void onNext(List<Model> models) { if(models.size() > 0){ progress.setVisibility(View.INVISIBLE); Log.d("count",String.valueOf(models.size())); 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); } } } 

Why is data not showing in recycler view?

5
  • One possibility that you might forget to set LayoutManager to recycler view .. Commented Feb 23, 2020 at 5:56
  • I have updated my answer and added layout manager but still data is not showing . Commented Feb 23, 2020 at 6:02
  • Just Use adapter = new PostAdapter(getApplicationContext(),models). Cause list is always Empty .. Commented Feb 23, 2020 at 6:21
  • 1
    @ADM oh, well spotted...I missed that one was models and one was list. This is definitely the problem, removed my previous suggestion. Commented Feb 23, 2020 at 6:26
  • After replacing list with models it is still showing empty. Commented Feb 23, 2020 at 6:29

1 Answer 1

0

First of all, create adapter in onCrete method of MainActivity. Then add into your adapter a method

public void setItems(List<Model> data){ list.clear() list.addAll(data) notifyDataSetChanged() } 

And use this method to add items into adapter.

myApi.getData().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<List<Model>>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(List<Model> models) { if(models.size() > 0){ progress.setVisibility(View.INVISIBLE); Log.d("count",String.valueOf(models.size())); adapter.setItems(models) } } @Override public void onError(Throwable e) { Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); } @Override public void onComplete() { } }); } 

Update your MainActivity method:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = findViewById(R.id.progress); recycler = findViewById(R.id.recycler); recycler.setHasFixedSize(true); recycler.setLayoutManager(new LinearLayoutManager(this)); adapter = new PostAdapter(this, new ArrayList()); recycler.setAdapter(adapter); fetchData(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

could you please tell me where do I place above method in my existing code
where do I add setItems() method because if I am adding it in MainActivity then it is not showing up with adapter.setItems().
add it into PostAdapter class
On doing so Its saying java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.apitest.PostAdapter.setItems(java.util.List)' on a null object reference

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.