Reputation: 11
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.sstatic.net/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
Upvotes: 0
Views: 563
Reputation: 27226
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.
Upvotes: 1