Whenever data is added in to the server, recycler view loads slowly, I fetch the data by using an API. When installing the app or if I add items in the database, it loads slowly for about 3-5 secs, But when I reopen the app it works fine. Database I am using is MySql phpadmin, I am using Volley library to fetch data from an API
Question
How to fasten the process of loading the data or is there any way to load the data in background so that whenever user open the app so that he doesn't have to wait for some seconds as mentioned above.
private void loadRecyclerViewData(){
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Please wait your stories are loading...");
progressDialog.show();
String URL_DATA = "https://.../webservices/api/getstories.php";
//fetch the data from the server
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
response -> {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("data");
for(int i =0; i<array.length(); i++){
JSONObject o = array.getJSONObject(i);
StoriesModel item = new StoriesModel (
o.getString("story"),
o.getString("conclusion"),
o.getString("rating"),
o.getString("author")
);
storiesModels.add(item);
}
mAdapter = new StoriesAdapter(storiesModels, getContext());
recyclerView.setAdapter(mAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> {
progressDialog.dismiss();
Toast.makeText(getContext(), (CharSequence) new VolleyError(), Toast.LENGTH_LONG).show();
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
Adapter class:-
public class StoriesAdapter extends RecyclerView.Adapter < StoriesAdapter.ViewHolder > {
private List < StoriesModel > storiesModels;
private final Context context;
public StoriesAdapter(List < StoriesModel > storiesModels, Context context) {
this.storiesModels = storiesModels;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StoriesModel storiesModel = storiesModels.get(position);
holder.authorName.setText(storiesModel.getAuthor());
holder.conclusion.setText(storiesModel.getConclusion());
holder.rating.setText(storiesModel.getRating());
holder.story.setText(storiesModel.getStory());
holder.relativeLayout.setOnClickListener(v - > {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("authorName", storiesModel.getAuthor());
intent.putExtra("conclusion", storiesModel.getConclusion());
intent.putExtra("rating", storiesModel.getRating());
intent.putExtra("story", storiesModel.getStory());
context.startActivity(intent);
});
}
@Override
public int getItemCount() {
return storiesModels.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final TextView authorName;
public final TextView conclusion;
public final TextView rating;
public final TextView story;
public final RelativeLayout relativeLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
authorName = itemView.findViewById(R.id.authorName);
conclusion = itemView.findViewById(R.id.conclusion);
rating = itemView.findViewById(R.id.rating);
story = itemView.findViewById(R.id.story);
relativeLayout = itemView.findViewById(R.id.Newlinear_layout);
}
}
}