display data into recycler view using mutiple api request and one adapter - java

I'm using RecyclerView to display some data from an API response using one adapter
but i want to display data from more then one API response (so i need to make more then one get request)
this is my adapter :
private Context Context1;
private List<TraitementTicketModel> followuplist;
public FollowupAdapter(Context mContext, List<TraitementTicketModel> followuplist) {
this.Context1 = mContext;
this.followuplist = followuplist;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v;
LayoutInflater layoutInflater=LayoutInflater.from(Context1);
v=layoutInflater.inflate(R.layout.followupitem,parent,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.users_id.setText(followuplist.get(position).getUsers_id());
holder.date.setText(followuplist.get(position).getDate());
holder.titre.setText(followuplist.get(position).getTitre());
holder.content.setText(html2text(followuplist.get(position).getContent()));
}
#Override
public int getItemCount() {
return followuplist.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView users_id;
TextView date;
TextView content;
TextView titre;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
users_id=itemView.findViewById(R.id.user_id_followup);
date =itemView.findViewById(R.id.date_followup);
content=itemView.findViewById(R.id.contenu_followup);
titre=itemView.findViewById(R.id.titre_followup);
}
}
public static String html2text(String html) {
return Jsoup.parse(Jsoup.parse(html).text()).text();
}
and this is the code to display the data into the recyclerview
SharedPreferences sp = getApplicationContext().getSharedPreferences("tokenPref", Context.MODE_PRIVATE);
String sestoken = sp.getString("token", "");
SharedPreferences sp1 = getApplicationContext().getSharedPreferences("idPref", Context.MODE_PRIVATE);
String id = sp1.getString("id", "");
Retrofit retrofit = RetrofitInstance.getRetrofitInstance();
final Api api = retrofit.create(Api.class);
Call<List<TraitementTicketModel>> call = api.getfollowup(id, sestoken);
call.enqueue(new Callback<List<TraitementTicketModel>>() {
#Override
public void onResponse(Call<List<TraitementTicketModel>> call, Response<List<TraitementTicketModel>> response) {
if (!response.isSuccessful()) {
Toast.makeText(getApplicationContext(),"Something is wrong !! ",Toast.LENGTH_LONG).show();
Log.e("TAG", "onResponse: something is wrong");
}
List<TraitementTicketModel> followups = response.body();
for (TraitementTicketModel followup : followups) {
followuplist.add(followup);
}
followuplist.add(firstfollowup());
PutDataIntoRecyclerView(followuplist);
}
#Override
public void onFailure(Call<List<TraitementTicketModel>> call, Throwable t) {
}
});
private void PutDataIntoRecyclerView(List<TraitementTicketModel> followuplist) {
FollowupAdapter adapter =new FollowupAdapter(this,followuplist);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
}
so is it possible to display data into one recylerview using one adapter and more then one API request .
Edit :
this is the TraitementTicketModel class :
public class TraitementTicketModel {
private String users_id;
private String date;
private String content;
private String titre;
public TraitementTicketModel(String users_id, String date, String content, String titre) {
this.users_id = users_id;
this.date = date;
this.content = content;
this.titre = titre;
}
public String getTitre() {
return titre;
}
public String getUsers_id() { return users_id; }
public String getDate() { return date; }
public String getContent() {
return content;
}
}
when i try to sort the list by date before i put it in the adapter the data doesn't show .
in the adapter class :
public List addToList(List<TraitementTicketModel> list) {
this.followuplist.addAll(list);
notifyDataSetChanged();
Collections.sort(followuplist, new Comparator<TraitementTicketModel>() {
#Override
public int compare(TraitementTicketModel o1, TraitementTicketModel o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
return this.followuplist;
}
in the main activity :
private void PutDataIntoRecyclerView(List<TraitementTicketModel> followuplist) {
Sort(followuplist);
if (adapter == null) {
adapter=new FollowupAdapter(this,followuplist);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
}else {
adapter=new FollowupAdapter(this,adapter.addToList(followuplist));
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
}
}
private List Sort (List<TraitementTicketModel> datalist){
Collections.sort(datalist, new Comparator<TraitementTicketModel>() {
#Override
public int compare(TraitementTicketModel o1, TraitementTicketModel o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
return datalist;
}

so is it possible to display data into one recylerview using one adapter and more then one api request
Yes it's possible if all API responses have the same data scheme as the adapter list.
You can add a method in the adapter that accumulates the current list:
public addToList(List<TraitementTicketModel> list) {
this.followuplist.addAll(list);
notifyDataSetChanged();
}
Then make the adapter as a class field, to only be instantiated for the first response, and reuse it in further responses:
FollowupAdapter mAdapter;
Then if the adapter not instantiated, instantiate it with the initial list, otherwise accumulate the list:
private void PutDataIntoRecyclerView(List<TraitementTicketModel> followuplist) {
if (mAdapter == null) {
mAdapter = new FollowupAdapter(this, followuplist);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
} else {
mAdapter.addToList(followuplist);
}
}

Related

recyclerView missing error in Android Studio

I'm a beginner programmer and I'm trying to receive data from the server using node.js and display the list on Android Studio but I keep having the following error:
E/RecyclerView: No adapter attached; skipping layout
D/HistoryActivity: HistoryResponse{result=[com.sooryong.loginexample.data.HistoryData#3303f3c, com.sooryong.loginexample.data.HistoryData#33764c5]}
D/AndroidRuntime: Shutting down VM
As you can see, I can't recall my list properly as well and I don't know how to fix it. Here are my codes:
public interface ServiceApi {
#POST("/user/login")
Call<LoginResponse> userLogin(#Body LoginData data);
#POST("/user/join")
Call<JoinResponse> userJoin(#Body JoinData data);
#GET("/test/history")
Call<HistoryResponse> getData();
}
HistoryData.java
public class HistoryData {
#SerializedName("eventID")
private int eventID;
#SerializedName("eventType")
private String eventType;
#SerializedName("eventDate")
private String eventDate;
#SerializedName("userID")
private int userID;
#SerializedName("sensorID")
private String sensorID;
public HistoryData(int eventID, String eventType, String eventDate, int userID, String sensorID) {
super();
this.eventID = eventID;
this.eventType = eventType;
this.eventDate = eventDate;
this.userID = userID;
this.sensorID = sensorID;
}
public int getEventID() {
return eventID;
}
public String getEventType() {
return eventType;
}
public String getEventDate() { return eventDate; }
public int getUserID() {
return userID;
}
public String getSensorID() { return sensorID; }
}
HistoryResponse.java
public class HistoryResponse {
#SerializedName("code")
public String code;
#SerializedName("message")
public String message;
#SerializedName("result")
public List<HistoryData> result;
#Override
public String toString() {
return "HistoryResponse{" + "result=" + result +'}';
}
}
HistoryActivity.java
public class HistoryActivity extends AppCompatActivity {
HistoryResponse dataList;
List<HistoryData> dataInfo;
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view);
dataInfo = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
ServiceApi apiInterface = RetrofitClient.getClient().create(ServiceApi.class);
Call<HistoryResponse> call = apiInterface.getData();
call.enqueue(new Callback<HistoryResponse>() {
#Override
public void onResponse(Call<HistoryResponse> call, Response<HistoryResponse> response) {
dataList = response.body();
Log.d("Response", dataList.toString());
dataInfo = dataList.result;
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), dataInfo);
recyclerView.setAdapter(recyclerAdapter);
}
#Override
public void onFailure(Call<HistoryResponse> call, Throwable t) {
Log.d("Response", t.getMessage());
}
});
}
}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
private Context c;
private List<HistoryData> dataList;
public RecyclerAdapter(Context c, List<HistoryData> dataList) {
this.c = c;
this.dataList = dataList;
}
#NonNull
#Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(c).inflate(R.layout.recycler_view, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerAdapter.MyViewHolder holder, int position) {
holder.id.setText(dataList.get(position).getSensorID());
holder.type.setText("" + dataList.get(position).getEventType());
holder.date.setText("" + dataList.get(position).getEventDate());
}
#Override
public int getItemCount() {
return (dataList == null) ? 0 : dataList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView id;
TextView type;
TextView date;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
id = (TextView)itemView.findViewById(R.id.sensorID);
type = (TextView)itemView.findViewById(R.id.eventType);
date = (TextView)itemView.findViewById(R.id.eventDate);
}
}
}
I don't think the data is being initialized properly inside the RecyclerAdapter class. Is it because I couldn't load the data properly from the server?
I ever face the same issue similiar to this.
What I've done was: initialize the adapter once only the activity created:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view);
dataInfo = new ArrayList<>();
initAdapter();
....
}
private void initAdapter() {
//The adapter should create once in onCreate
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext, LinearLayoutManager.VERTICAL, false);
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), dataInfo);
recyclerView.setAdapter(recyclerAdapter);
}
Then when in callback result: you can clear the previous data before reload to the adapter:
call.enqueue(new Callback<HistoryResponse>() {
#Override
public void onResponse(Call<HistoryResponse> call, Response<HistoryResponse> response) {
dataList = response.body();
dataInfo.clear(); // <- here we clear all previous data before reload result to the adapter.
dataInfo = dataList.result;
recyclerAdapter.notifyDataSetChanged()
#Override
public void onFailure(Call<HistoryResponse> call, Throwable t) {
Log.d("Response", t.getMessage());
}
});
Hope this help.

change value of textview in a recyclerview item

guys I'm using multiple api request with the same response and displaying it into a recycling
view
but in those response I get a user_id instead of user name but I need to display the user name into the recyclerview
this image breaks down what i need to do : https://i.stack.imgur.com/IdQ6e.png
so this is my adapter class where I tried to make a hashmap using another api request and making a hashmap with <user_id,user_name> and trying to use it in the holder class to display the user name but it didn't work :
public class FollowupAdapter extends RecyclerView.Adapter {
Map<String, String> userMap1 = new HashMap<>();
private Context Context1 ;
private List<TraitementTicketModel> followuplist;
public FollowupAdapter(Context mContext, List<TraitementTicketModel> followuplist) {
this.Context1 = mContext;
this.followuplist = followuplist;
}
#Override
public int getItemViewType(int position) {
if(followuplist.get(position).getTaskcategories_id()!=null) {
return 3;
}else if (followuplist.get(position).getSolutiontypes_id()!=null){
return 2;
}
return 1;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater=LayoutInflater.from(Context1);
if (viewType==1){
view = layoutInflater.inflate(R.layout.followupitem, parent,false);
return new ViewHolderFollowup(view);
}else if (viewType==2){
view = layoutInflater.inflate(R.layout.solutionitem, parent,false);
return new ViewHolderSolution(view);
}
else
view = layoutInflater.inflate(R.layout.taskitem, parent,false);
return new ViewHolderTask(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
displayusers();
if(followuplist.get(position).getTaskcategories_id()!=null) {
ViewHolderTask viewHolderTask = (ViewHolderTask) holder;
viewHolderTask.users_id.setText(userMap1.get(followuplist.get(position).getUsers_id()));
viewHolderTask.date.setText(followuplist.get(position).getDate());
viewHolderTask.content.setText(html2text(followuplist.get(position).getContent()));
}else if(followuplist.get(position).getSolutiontypes_id()!=null){
ViewHolderSolution viewHolderSolution = (ViewHolderSolution) holder;
viewHolderSolution.users_id.setText(userMap1.get(followuplist.get(position).getUsers_id()));
viewHolderSolution.date.setText(followuplist.get(position).getDate());
viewHolderSolution.content.setText(html2text(followuplist.get(position).getContent()));
}
else {
ViewHolderFollowup viewHolderFollowup = (ViewHolderFollowup) holder;
viewHolderFollowup.users_id.setText(userMap1.get(followuplist.get(position).getUsers_id()));
viewHolderFollowup.content.setText(html2text(followuplist.get(position).getContent()));
viewHolderFollowup.titre.setText(followuplist.get(position).getTitre());
viewHolderFollowup.date.setText(followuplist.get(position).getDate());
}
}
#Override
public int getItemCount() { return followuplist.size(); }
public static class ViewHolderFollowup extends RecyclerView.ViewHolder{
TextView users_id;
TextView date;
TextView content;
TextView titre;
public ViewHolderFollowup(#NonNull View itemView) {
super(itemView);
users_id=itemView.findViewById(R.id.user_id_followup);
date =itemView.findViewById(R.id.date_followup);
content=itemView.findViewById(R.id.contenu_followup);
titre=itemView.findViewById(R.id.titre_followup);
}
}
public static class ViewHolderTask extends RecyclerView.ViewHolder {
TextView users_id;
TextView date;
TextView content;
public ViewHolderTask(#NonNull View itemView) {
super(itemView);
users_id=itemView.findViewById(R.id.user_id_task);
date =itemView.findViewById(R.id.date_task);
content=itemView.findViewById(R.id.contenu_task);
}
}
public static class ViewHolderSolution extends RecyclerView.ViewHolder{
TextView users_id;
TextView date;
TextView content;
public ViewHolderSolution(#NonNull View itemView) {
super(itemView);
users_id=itemView.findViewById(R.id.user_id_solution);
date =itemView.findViewById(R.id.date_solution);
content=itemView.findViewById(R.id.contenu_solution);
}
}
public static String html2text(String html) {
return Jsoup.parse(Jsoup.parse(html).text()).text();
}
public List addToList(List<TraitementTicketModel> list) {
if(this.followuplist.addAll(list)){
Sort(followuplist);
}
notifyDataSetChanged();
return this.followuplist;
}
private List Sort (List<TraitementTicketModel> datalist){
Collections.sort(datalist, new Comparator<TraitementTicketModel>() {
#Override
public int compare(TraitementTicketModel o1, TraitementTicketModel o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
return datalist;
}
private void displayusers() {
SharedPreferences sp =Context1.getApplicationContext().getSharedPreferences("tokenPref", Context.MODE_PRIVATE);
String sestoken = sp.getString("token","");
Retrofit retrofit= RetrofitInstance.getRetrofitInstance();
final Api api= retrofit.create(Api.class);
Call<List<User>> call = api.getUser(sestoken);
call.enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
if (response.code() != 200){
Log.e("TAG", "onResponse: something is wrong"+response.code() );
}
List<User> users = response.body();
for (User user : users) {
if (userMap.get(user.getId()) == null)
userMap.put(user.getId(), user.getName());
}
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
}
});
}
private Map<String, String> PutMap(HashMap<String, String> userMap) {
userMap1=userMap;
return userMap1;
}
}
this hashmap I tried didn't work appreciate any solution or idea I'm really stuck in this
Your adapter's job is to adapt a data model into a series of view holders, not to transform, combine, fetch, mutate, etc. a different series of data coming from different sources.
Ask yourself this question: if you could "magically" call an object like this repository.getList() that would return a List of Something and that Something had three fields:
class Something {
String id;
String userName;
String content;
String type; //maybe useful for the Adapter to pick what type...
... anything else you need
}
So you have a List<Something>() that you can pass to your adapter, wouldn't your adapter suddenly be super simple?
That's what you need to do; remove all the logic that does not belong in the adapter (I expect only to have a onCreateViewHolder onBindViewHolder getItemViewType and that's it).
You're making a big problem into your adapter, which already has a lot of other responsibilities to carry. This data problem (putting the user + id + content) in a single object is a problem of your data source/repository, and not the adapter of a recycler view.
Transform your data before you pass it to your adapter. Your life, your fellow developers, and the future you, will be grateful.

How can I hide an ImageView on my RecyclerView for certain items based on a particular condition?

I have a RecyclerView with an ImageView being a part of the items. I want to hide the ImageView from an item in the RecyclerView if a certain condition is met. How can I do that? I am attaching the image of how I want it to look like.
I am just defining the ImageViews in my xml layout file, so I cannot figure out how to actually remove it based on a certain condition in my android activity. I am attaching the code for the adapter class and my activity as well.
Here is the code for my adapter class
Adapter Class
public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ReportViewHolder> {
private ArrayList<ReportItem> reportlist;
private OnItemClickListener mListener;
private Context mContext;
public ReportAdapter(ArrayList<ReportItem> reportlist, Context context) {
this.reportlist = reportlist;
this.mContext = context;
}
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public static class ReportViewHolder extends RecyclerView.ViewHolder {
public TextView departureDate;
public TextView flightNumber;
public View relativelayout;
public ReportViewHolder(#NonNull View itemView, OnItemClickListener listener, Context context) {
super(itemView);
departureDate = itemView.findViewById(R.id.departureDaterecyclerview);
flightNumber = itemView.findViewById(R.id.flightnumberrecyclerview);
relativelayout = itemView.findViewById(R.id.relativeLayoutReports);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
#NonNull
#Override
public ReportViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.report_listing_item, parent, false);
ReportViewHolder rvh= new ReportViewHolder(v,mListener,mContext);
return rvh;
}
#SuppressLint("ResourceAsColor")
#Override
public void onBindViewHolder(#NonNull ReportViewHolder holder, int position) {
ReportItem currentItem = reportlist.get(position);
if(position%2==0){
holder.relativelayout.setBackgroundColor(mContext.getResources().getColor(R.color.reportlistingteal));
} else {
holder.relativelayout.setBackgroundColor(mContext.getResources().getColor(R.color.reportlistinglightteal));
}
holder.departureDate.setText((currentItem.getDepartureDate()));
holder.flightNumber.setText(currentItem.getFlightNumber());
}
Here is the code for my activity file
Activity file
public class ReportListingActivity extends AppCompatActivity {
private Button uploadAllBtn;
private EditText searchFlights;
private RecyclerView mRecyclerView;
private ReportAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<ReportItem> reportitems = new ArrayList<>();
private FlightViewModel flightViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_listing);
uploadAllBtn = findViewById(R.id.uploadAllReports);
searchFlights = findViewById(R.id.searchFlightText);
mRecyclerView = findViewById(R.id.recyclerView);
flightViewModel = new ViewModelProvider(this).get(FlightViewModel.class);
flightViewModel.getAllFlights().observe(this, new Observer<List<Flight>>() {
#Override
public void onChanged(List<Flight> flight_list) {
if (flight_list.size() == 0) return;
String flightno = flight_list.get(0).getFlightNumber();
String flightdate = flight_list.get(0).getDate();
String[] flight_details = new String[2];
flight_details[0]= flightno;
flight_details[1] = flightdate;
Log.v("pp", flight_details[0]);
for(int i = 0; i <flight_list.size();i++){
String flightnumber = flight_list.get(i).getFlightNumber();
String departuredate = flight_list.get(i).getDate();
reportitems.add(new ReportItem(flightnumber,departuredate));
}
mRecyclerView.getAdapter().notifyDataSetChanged();
flightViewModel.getAllFlights().removeObservers(ReportListingActivity.this);
}
});
mLayoutManager = new LinearLayoutManager(ReportListingActivity.this);
mAdapter = new ReportAdapter(reportitems, ReportListingActivity.this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
Report Item
public class ReportItem {
private String departureDate;
private String flightNumber;
public ReportItem(String departureDate, String flightNumber) {
this.departureDate = departureDate;
this.flightNumber = flightNumber;
}
public String getDepartureDate() {
return departureDate;
}
public String getFlightNumber() {
return flightNumber;
}
}
Add a boolean flag to your ReportItem class for each RecyclerView item. You will need to specify which rows show or hide this field when each item is created:
public class ReportItem {
private String departureDate;
private String flightNumber;
private Boolean showMailIcon;
public ReportItem(String departureDate, String flightNumber, Boolean showMailIcon) {
this.departureDate = departureDate;
this.flightNumber = flightNumber;
this.showMailIcon = showMailIcon
}
public String getDepartureDate() {
return departureDate;
}
public String getFlightNumber() {
return flightNumber;
}
public String getShowMailIcon() {
return showMailIcon;
}
}
Then update the onBindViewHolder() method override to use this flag to show/hide the ImageView:
#Override
public void onBindViewHolder(#NonNull ReportViewHolder holder, int position) {
ReportItem currentItem = reportlist.get(position);
if (currentItem.getShowMailIcon() == true) {
holder.mailIcon.setVisibility(View.VISIBLE);
} else {
holder.mailIcon.setVisibility(View.GONE);
}
//.......
}

Viewmodel shows empty recyclerview when activity rotated

I am pretty new to the Android architecture components and have been trying out room for data storage from my server. Problem is no data is being shown on the recycler view IMMEDIATELY. There's a searchview(no logic implemented just there) right above my recyclerview and when I click searchview for input, the recyclerview shows all the data which was supposed to be shown earlier.
RestaurantsAdapter:
public class RestaurantsAdapter extends RecyclerView.Adapter<RestaurantsAdapter.MyViewHolder> {
private List<Restaurant> data;
private Context context;
private LayoutInflater layoutInflater;
private final Random r = new Random();
public RestaurantsAdapter(Context context) {
this.data = new ArrayList<>();
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public RestaurantsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_restaurant, parent, false);
return new RestaurantsAdapter.MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RestaurantsAdapter.MyViewHolder holder, int position) {
holder.rName.setText(data.get(position).getName());
}
public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);
data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
}
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView rName;
public MyViewHolder(View itemView) {
super(itemView);
rName = (TextView) itemView.findViewById(R.id.restaurant_name);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
}
}
class RestaurantDiffCallback extends DiffUtil.Callback {
private final List<Restaurant> oldRestaurants, newRestaurants;
public RestaurantDiffCallback(List<Restaurant> oldPosts, List<Restaurant> newPosts) {
this.oldRestaurants = oldPosts;
this.newRestaurants = newPosts;
}
#Override
public int getOldListSize() {
return oldRestaurants.size();
}
#Override
public int getNewListSize() {
return newRestaurants.size();
}
#Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldRestaurants.get(oldItemPosition).getIdentifier().equals(newRestaurants.get(newItemPosition).getIdentifier());
}
#Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldRestaurants.get(oldItemPosition).equals(newRestaurants.get(newItemPosition));
}
}}
MainActivity:
public class MainActivity extends AppCompatActivity {
private RestaurantsAdapter restaurantsAdapter;
private RestaurantViewModel restaurantViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
restaurantsAdapter = new RestaurantsAdapter(this);
restaurantViewModel = ViewModelProviders.of(this).get(RestaurantViewModel.class);
restaurantViewModel.getAllRestaurants().observe(this, restaurants -> restaurantsAdapter.setData(restaurants));
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(restaurantsAdapter);
}}
ViewModel:
public class RestaurantViewModel extends AndroidViewModel {
private RestaurantDao restaurantDao;
private ExecutorService executorService;
private ApiInterface webService;
public RestaurantViewModel(#NonNull Application application) {
super(application);
restaurantDao = RestaurantsDatabase.getInstance(application).restaurantDao();
executorService = Executors.newSingleThreadExecutor();
webService = ApiClient.getApiClient().create(ApiInterface.class);
}
LiveData<List<Restaurant>> getAllRestaurants() {
refreshUser();
return restaurantDao.findAll();
}
private void refreshUser() {
executorService.execute(() -> {
int numOfRestaurants = restaurantDao.totalRestaurants();
if (numOfRestaurants < 30) {
Call<RestaurantsModel> call = webService.getRestaurants();
call.enqueue(new Callback<RestaurantsModel>() {
#Override
public void onResponse(#NonNull Call<RestaurantsModel> call, #NonNull Response<RestaurantsModel> response) {
restaurantDao.saveAll(response.body().getData().getData());
}
#Override
public void onFailure(#NonNull Call<RestaurantsModel> call, #NonNull Throwable t) {
}
});
}
});
}}
If you don't use the DiffUtil with its diffResult.dispatchUpdatesTo(this); you should do notifyDataSetChanged(). In your case, in RestaurantsAdapter.setData add one line:
// first initialization
data = newData;
notifyDataSetChanged();
You have an issue in your setData method:
public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);
data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
}
}
When your newData is null your change the data source of your adapter, but you don't call notifyDataSetChanged.
This way the data that you are seeing on the screen will not be updated.
So in order to fix it:
public void setData(List<Restaurant> newData) {
if (data != null) {
RestaurantDiffCallback restaurantDiffCallback = new RestaurantDiffCallback(data, newData);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(restaurantDiffCallback);
data.clear();
data.addAll(newData);
diffResult.dispatchUpdatesTo(this);
} else {
// first initialization
data = newData;
notifyDataSetChanged();
}
}
Another thing, if not a very good practice setting your adapter dataset has null. So my suggestion is to set your data as an empty list instead of null:
data = new ArrayList<>();
The issue: you're using ViewModels incorrectly. You are only returning data from the ViewModel, but never saving data in the ViewModel
Start by reading how ViewModels work here: https://developer.android.com/topic/libraries/architecture/viewmodel

How to set data in recyclerView with Retrofit in Android

I want to show some data into recyclerview and for this I am using the code displayed below.
When I run the application, it's not showing me any data into recyclerview and ListData.size() is 0, but in PostMan
I write below code for set into recyclerView :
InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
Call<CommentResponse> call = api.getComments(sendData);
call.enqueue(new Callback<CommentResponse>() {
#Override
public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
if (response.body().getData() != null) {
commentModel.clear();
commentModel.addAll(response.body().getData());
commentsListAdapter.notifyDataSetChanged();
content_newsCommentsRecyclerView.setAdapter(commentsListAdapter);
}
}
#Override
public void onFailure(Call<CommentResponse> call, Throwable t) {
}
});
My Adapter codes:
public class CommentsListAdapter extends RecyclerView.Adapter<CommentsListAdapter.ViewHolder> {
private Context context;
private List<CommentData> model;
public CommentsListAdapter(Context context, List<CommentData> model) {
this.context = context;
this.model = model;
}
#Override
public CommentsListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_comment, parent, false);
return new CommentsListAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(final CommentsListAdapter.ViewHolder holder, final int position) {
holder.row_commentNameTxt.setText(Html.fromHtml(model.get(position).getOwner().getName()));
holder.row_commentCommentTxt.setText(Html.fromHtml(model.get(position).getText()));
Glide.with(context)
.load(model.get(position).getOwner().getImageUrl())
.placeholder(R.drawable.default_image)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(holder.row_commentProfileImage);
holder.row_commentLikeTxt.setText(model.get(position).getLikeCount() + "");
holder.row_commentReplayTxt.setText(model.get(position).getRepliesCount() + "");
holder.row_commentDateTxt.setText(model.get(position).getSubmitDate() + " " + model.get(position).getSubmitTime());
}
#Override
public int getItemCount() {
return model.size();
}
public void addNewItem(List<CommentData> newContent) {
int start = this.model.size();
int end = newContent.size();
model.addAll(newContent);
notifyDataSetChanged();
}
public void clear() {
model.clear();
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView row_commentProfileImage;
private TextView row_commentNameTxt, row_commentCommentTxt, row_commentLikeTxt, row_commentReplayTxt, row_commentDateTxt;
public ViewHolder(View itemView) {
super(itemView);
row_commentProfileImage = (CircleImageView) itemView.findViewById(R.id.row_commentProfileImage);
row_commentNameTxt = (TextView) itemView.findViewById(R.id.row_commentNameTxt);
row_commentCommentTxt = (TextView) itemView.findViewById(R.id.row_commentCommentTxt);
row_commentLikeTxt = (TextView) itemView.findViewById(R.id.row_commentLikeTxt);
row_commentReplayTxt = (TextView) itemView.findViewById(R.id.row_commentReplayTxt);
row_commentDateTxt = (TextView) itemView.findViewById(R.id.row_commentDateTxt);
}
}
}
How can I display the data into recyclerView?
Create a method inside your Adapter class
//This will add all the items in the adapter's list
public void addAllItems(List<CommentData> items) {
model.addAll(items);
notifyDataSetChanged();
}
//In Adapter's Constructor do the following changes
public CommentsListAdapter(Context context, List<CommentData> model) {
this.context = context;
this.model = new ArrayList<>;
}
and when you are fetching your response you can call this by
//Inside your onCreate add the below code
mAdapter = new CommentsListAdapter (this);
content_newsCommentsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
content_newsCommentsRecyclerView.setAdapter(mAdapter);
//Call this inside your success of onResponse
commentsListAdapter.addAllItems(commentModel);
This will update the content of recyclerView and notify the changes made, do try this and let me know if you have any issue.
Before the API call initialize the Adapter
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRVCommentList.setLayoutManager(mLayoutManager);
CommentsListAdapter commentsListAdapter= new CommentsListAdapter(this)
mRVCommentList.setAdapter(commentsListAdapter)
Then call the API
public void onResponse(Call<CommentResponse> call, Response<CommentResponse> response) {
if (response.body().getData() != null) {
List<CommentData> list= response.body().getData().getCommentData();
commentsListAdapter.setData(list);
}
}
Small changes in Adapter Class
private List<CommentData> commentData;
public CommentsListAdapter(Context context) {
this.context = context;
this.commentData = new ArrayList<>();
}
public void setData(List<CommentData> commentData) {
this.commentData= commentData;
notifyDataSetChanged();
}

Categories