简体   繁体   English

合并两个不同对象的列表

[英]merge two lists of different objects

i'm using api requests that returns a list.我正在使用返回列表的 api 请求。 -the first api request returns a list of object that contains (user_id,content,date,title) -the second response returns list of object too that contains (user_id,user_name). - 第一个 api 请求返回包含 (user_id,content,date,title) 的对象列表 - 第二个响应也返回包含 (user_id,user_name) 的对象列表。

i want to merge the two list the display them into one recycler view but keep user name instead of user_id.this image breaks down what i want clearly.我想将两个列表合并到一个回收者视图中,但保留用户名而不是 user_id。这张图片清楚地分解了我想要的内容。 在此处输入图片说明

apprecuiate any help i'm really stuck in this and i need it ty .欣赏任何帮助,我真的被困在这个问题上,我需要它。

EDIT编辑

this is the first api call :这是第一个 api 调用:

    followuplist=new ArrayList<>();


    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");


            } else if (response.body() == null) {

                return;
            }

            List<TraitementTicketModel> followups = response.body();


            for (TraitementTicketModel followup : followups) {


                followuplist.add(followup);

            }


            followuplist.add(firstfollowup());
           

        }

        @Override
        public void onFailure(Call<List<TraitementTicketModel>> call, Throwable t) {
            Toast.makeText(getApplicationContext(),"Pas de connextion internet",Toast.LENGTH_LONG).show();
        }
    });

this is the second api call :这是第二个 api 调用:

      List<User> userList;
      SharedPreferences sp =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){

                userList.add(user);
            }

            swipeRefreshLayout.setRefreshing(false);



        }

so I have two liststhe first one is : followuplist (user_id,title,content,date) and the second : userList(user_id,user_name) but i didn't know what to do after that to get to my goal所以我有两个列表,第一个是:followuplist (user_id,title,content,date) 和第二个:userList(user_id,user_name) 但我不知道在那之后该怎么做才能达到我的目标

You can do something like that.你可以做类似的事情。 In this example UserDetails is the object on the left in your image, UserInfo the one on the right, and MergeData the result.在此示例中, UserDetails是图像左侧的对象, UserInfo是右侧的对象, MergeData是结果。

You should use Kotlin instead of Java, it's far easier to manipulate lists.您应该使用 Kotlin 而不是 Java,它更容易操作列表。

 List<MergedData> mergeList(
            List<UserDetails> listUserDetails,
            List<UserInfo> listUserInfo
    ) {

        // Resulting list
        final List<MergedData> result = new ArrayList<>();
        // We iterate through the first list
        for (UserDetails details : listUserDetails) {
            // For each element of the list we will try to find one with the same user id in the other list
            for (UserInfo info : listUserInfo) {
                // if the current element of the second list has the same user id as the current one from the first list, we merge the data in a new object and this object is then added to the result list.
                if (details.getUserId().equals(info.getUserId())) {
                    result.add(
                            new MergedData(
                               info.getName(),
                               details.getContent(),
                               details.getTitre(),
                               details.getDate()
                            )
                    );
                    // Once the object is found it is unnecessary to continue looping though the second list, so we break the for loop.
                    break;
                }
            }
        }
        // Once we finished to iterate through the first list, we return the result.
        return result;
    }

Same example in Kotlin: Kotlin 中的相同示例:

fun mergeList(
    listUserDetails: List<UserDetails>,
    listUserInfo: List<UserInfo>
): List<MergedData> =
    listUserDetails.mapNotNull { details ->
        listUserInfo
            .firstOrNull { it.userId == details.userId }
            ?.let { info ->
                MergedData(
                    info.name,
                    details.content,
                    details.titre,
                    details.date
                )
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM