1

I am getting responses from two different JSON API's where a id object is common in both the API's. I want to know how many times does a id from first response occurs in second response. like if id="642" from first response occurs two times in second response, then count =2. Like this for each id and then set it in side the recycler view for each positions for txtCountNo inside adapter.

First Response:

[{
    "id": "642"
    "Full_name": "Harsh",
},
{
  
    "id": "91"
    "Full_name": "Rahul",
}]

Second Response:

[{
    "Uniq_id": "36",
    "id": "91"
},
{
    "Uniq_id": "37",
    "id": "642"
},
{
    "Uniq_id": "38",
    "id": "642"
},
{
    "Uniq_id": "39",
    "id": "91"
}]

I have shown name using this code inside 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);


    }
}}

Photography Model:

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 Model:

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;
}}

Calling First 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();

        }
    });
}

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

        }
    });
}

Any help would be appreciated. Thanks.

2 Answers2

0

You can do something like this,

Create one hashMap of Integer and Integer. Call the second API and iterate through all the items that you received in response. Fill HashMap with this details like add id as the key and add value 1 by default. But before that search in the HashMap that particular id is exists or not if it existas already then update that id and set value by incrementing the current value and If it does not exist tnen simply put value as 1.

Now You have a HashMap ready with the count for appropriate ids.

Now call the first API and from HashMap, You would be able to get the count for that id. Otherwise You can add one more field for count in model class and save the count at the time of response

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • i have added the calling code for both api's can you, write the code for what you have commented by looking at my code. – Pratik Chauhan Feb 22 '21 at 10:31
  • Sorry @PratikChauhan but I don't have that much time. You can try step by step what I have mentioned here and If you stuck at any point then find a solution online. – Bhargav Thanki Feb 23 '21 at 04:41
0

Your model class which you have created that is basically for reading Response 1 and Response 2.

What if you create a different class which is like the UI which needs to be displayed. so say if Response 1 gives me ID and Name and Response 2 gives me ID and COUNT Then I would simply create a different class with ID NAME and COUNT all in a single class file.

Then when I call API 1 for response I will store the relevant data in relevant variable of the class which was created according to UI. Same process Ill do it for SECOND Response.

Once I have the List of API with both the Response I will set the adapter.

OR

I can even setAdapter on first API Call and on second API call simply notifyDatasetChanged.

Rahul Gupta
  • 146
  • 1
  • 9