微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过匹配来自两个 API 的 ID 将来自两个 JSON API 的响应查看到单个回收器视图适配器中?

如何解决如何通过匹配来自两个 API 的 ID 将来自两个 JSON API 的响应查看到单个回收器视图适配器中?

我收到来自两个不同 JSON API 的响应,其中 id 对象在这两个 API 中都很常见。 我想知道第一个响应中的 id 在第二个响应中出现多少次。 就像如果第一个响应中的 id="642" 在第二个响应中出现两次,则计数 =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",{
    "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;
}}

喜欢模特:

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) {

        }
    });
}

任何帮助将不胜感激。 谢谢。

解决方法

你可以这样做,

创建一个 Integer 和 Integer 的 hashMap。 调用第二个 API 并遍历您在响应中收到的所有项目。使用此详细信息填充 HashMap,例如添加 id 作为键并默认添加值 1。但是在 HashMap 中搜索该特定 id 之前是否存在,如果它已经存在,则通过增加当前值来更新该 id 并设置值,如果它不存在,则只需将值设为 1。

现在您已准备好一个 HashMap,其中包含适当 id 的计数。

现在调用第一个 API 并从 HashMap 中获取该 ID 的计数。 否则你可以在模型类中增加一个计数字段,并在响应时保存计数

,

您创建的模型类主要用于阅读响应 1 和响应 2。

如果你创建一个不同的类,就像需要显示的 UI 一样。 所以说如果响应 1 给我 ID 和名称和响应 2 给我 ID 和计数 然后我会在一个类文件中简单地创建一个不同的类,其中 ID NAME 和 COUNT 全部。

然后,当我调用 API 1 进行响应时,我会将相关数据存储在根据 UI 创建的类的相关变量中。 同样的过程我会为第二个响应做。

一旦我拥有带有响应的 API 列表,我将设置适配器。

我什至可以在第一个 API 调用和第二个 API 调用中设置适配器,只需 notifyDatasetChanged。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。