使用多个 api 请求和一个适配器将数据显示到 recyclerview
display data into recycler view using mutiple api request and one adapter
我正在使用 RecyclerView
通过一个适配器显示来自 API 响应的一些数据
但我想显示来自不止一个 API 响应的数据(所以我需要发出不止一个 get 请求)
这是我的适配器 :
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();
}
这是将数据显示到 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);
}
那么是否可以使用一个适配器将数据显示到一个 recylerview
中,然后是一个 API 请求。
编辑:
这是 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;
}
}
当我尝试在将列表放入适配器之前按日期对列表进行排序时,数据不显示。
在适配器中 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;
}
主要 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
是的,如果所有 API 响应都具有与适配器列表相同的数据方案。
你可以在adapter中添加一个方法来累积当前列表:
public addToList(List<TraitementTicketModel> list) {
this.followuplist.addAll(list);
notifyDataSetChanged();
}
然后将适配器作为 class 字段,仅在第一次响应时实例化,并在以后的响应中重用它:
FollowupAdapter mAdapter;
然后如果适配器没有实例化,用初始列表实例化它,否则累积列表:
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);
}
}
我正在使用 RecyclerView
通过一个适配器显示来自 API 响应的一些数据
但我想显示来自不止一个 API 响应的数据(所以我需要发出不止一个 get 请求)
这是我的适配器 :
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();
}
这是将数据显示到 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);
}
那么是否可以使用一个适配器将数据显示到一个 recylerview
中,然后是一个 API 请求。
编辑:
这是 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;
}
}
当我尝试在将列表放入适配器之前按日期对列表进行排序时,数据不显示。
在适配器中 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;
}
主要 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
是的,如果所有 API 响应都具有与适配器列表相同的数据方案。
你可以在adapter中添加一个方法来累积当前列表:
public addToList(List<TraitementTicketModel> list) {
this.followuplist.addAll(list);
notifyDataSetChanged();
}
然后将适配器作为 class 字段,仅在第一次响应时实例化,并在以后的响应中重用它:
FollowupAdapter mAdapter;
然后如果适配器没有实例化,用初始列表实例化它,否则累积列表:
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);
}
}