我从两个不同的JSON API中得到了响应,其中id对象在两个API中都是通用的。我想知道第一个响应中的id在第二个响应中出现了多少次。例如,如果来自第一个响应的计数“642”在第二个响应中出现两次,则id= =2。对于每个id,然后将其设置在回收器视图的侧面,用于txtCountNo内部适配器的每个位置。
第一个响应:
[{
"id": "642"
"Full_name": "Harsh",
},
{
"id": "91"
"Full_name": "Rahul",
}]
第二个响应:
[{
"Uniq_id": "36",
"id": "91"
},
{
"Uniq_id": "37",
"id": "642"
},
{
"Uniq_id": "38",
"id": "642"
},
{
"Uniq_id": "39",
"id": "91"
}]
我已经在Adapter中使用以下代码显示了名称:
public class PhotographyAdapter extends RecyclerView.Adapter<PhotographyAdapter.MyViewHolder> {
List<PhotographyModel> photographyList;
public PhotographyAdapter(List<PhotographyModel> photographyList, Context context) {
this.photographyList = photographyList;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_feed_post, parent, false);
return new MyViewHolder(view);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.txtUserName.setText(photographyList.get(position).getFullName())
}
@Override
public int getItemCount() {
return photographyList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtUserName,txtCountNo;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
txtUserName = itemView.findViewById(R.id.txtUserName);
txtCountNo = itemView.findViewById(R.id.txtCountNo);
}
}}
摄影模型:
public class PhotographyModel{
@SerializedName("Full_name")
@Expose
private String fullName;
@SerializedName("id")
@Expose
private String Id;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id =Id;
}}
Like模型:
public class LikeModel {
@SerializedName("Uniq_id")
@Expose
private String uniqId;
@SerializedName("id")
@Expose
private String Id;
public String getUniqId() {
return uniqId;
}
public void setUniqId(String uniqId) {
this.uniqId = uniqId;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}}
调用第一个API:
public void getFirstApiResponse() {
progressBar.setVisibility(View.VISIBLE);
Call<List<PhotographyModel>> ourSupplierResponseCall = RestClient.getClient().getPhotographyPosts();
ourSupplierResponseCall.enqueue(new Callback<List<PhotographyModel>>() {
@Override
public void onResponse(Call<List<PhotographyModel>> call, Response<List<PhotographyModel>> response) {
if (response.isSuccessful()) {
progressBar.setVisibility(View.GONE);
Log.d(TAG, "onResponse: " + response.toString());
List<Photography> photographyList1;
photographyAdapter = new PhotographyAdapter(photographyList1,getContext());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
rvPhotographyFragment.setLayoutManager(layoutManager);
rvPhotographyFragment.setAdapter(photographyAdapter);
}
}
@Override
public void onFailure(Call<List<PhotographyModel>> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
调用第二个API:
public void getSecondApiResponse() {
Call<List<Like>> responseCall = RestClient.getClient().getLikes();
responseCall.enqueue(new Callback<List<Like>>() {
@Override
public void onResponse(Call<List<Like>> call, Response<List<Like>> responseLike) {
responseLike.body();
Log.d(TAG, "onResponse: " + responseLike.body().toString());
}
@Override
public void onFailure(Call<List<Like>> call, Throwable t) {
}
});
}
任何帮助都将不胜感激。谢谢。
发布于 2021-02-22 10:02:19
你可以这样做,
创建一个整数和整数的hashMap。调用第二个API并迭代您在响应中收到的所有项。使用此详细信息填充HashMap,如添加id作为键,默认情况下添加值1。但是在HashMap中搜索该特定id之前,如果它已经存在,则更新该id,并通过递增当前值来设置该值,如果不存在,则简单地将值设置为1。
现在,您已经准备好了一个HashMap,其中包含适当ids的计数。
现在调用第一个API,并从HashMap中获得该id的计数。否则,您可以在模型类中为count再添加一个字段,并在响应时保存计数
发布于 2021-02-22 12:18:50
您创建的模型类主要用于读取Response 1和Response 2。
如果您创建了一个不同的类,比如需要显示的UI,该怎么办?因此,假设响应1给出了ID和名称,响应2给出了ID和COUNT,那么我只需在单个类文件中创建一个具有ID、Name和COUNT的不同类。
然后,当我调用API 1进行响应时,我会将相关数据存储在根据UI创建的类的相关变量中。同样的过程我会在第二次响应时这样做。
一旦我有了两个响应的API列表,我将设置适配器。
或
我甚至可以在第一次API调用时使用setAdapter,在第二次API调用时简单地使用notifyDatasetChanged。
https://stackoverflow.com/questions/66313193
复制相似问题