【问题标题】:display data into recycler view using mutiple api request and one adapter使用多个 api 请求和一个适配器将数据显示到 recyclerview
【发布时间】:2023-03-17 23:07:01
【问题描述】:

我正在使用RecyclerView 显示来自使用一个适配器的 API 响应的一些数据 但我想显示来自多个 API 响应的数据(因此我需要发出多个获取请求) 这是我的适配器:

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

}

那么是否可以使用一个适配器和多个 API 请求将数据显示到一个 recylerview 中。

编辑:

这是 TraitementTicketModel 类:

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

}

当我尝试在将列表放入适配器之前按日期对列表进行排序时,数据不会显示。

在适配器类中:

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

在主要活动中:

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

【问题讨论】:

    标签: java android api android-recyclerview adapter


    【解决方案1】:

    那么是否可以使用一个适配器和多个 api 请求将数据显示到一个 recylerview 中

    是的,如果所有 API 响应都具有与适配器列表相同的数据方案,这是可能的。

    您可以在适配器中添加一个累积当前列表的方法:

    public addToList(List<TraitementTicketModel> list) {
        this.followuplist.addAll(list);
        notifyDataSetChanged();
    }   
    

    然后将适配器作为一个类字段,只为第一个响应实例化,并在进一步的响应中重用它:

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

    【讨论】:

    • 它实际上可以工作,但是当我尝试对列表进行排序时,在我将其放入适配器之前尝试对列表进行排序时,数据没有显示您对此有什么想法
    • @LaroussiAziz 如果列表存在,那么排序不会是不显示它的原因,除非排序算法或同步问题存在问题。我鼓励你打开一个新问题完整的细节,以便有人可以提供帮助。祝你好运。
    • 好的,我会这样做的,但我确实是这个问题,因为我在另一个列表上的其他类中使用了相同的排序方法,它工作正常,但在这个合并列表中它不起作用。
    • 所以,可能是别的东西。当您打开问题时,请随时给我留言,以便我能很好地理解它
    • 已编辑此问题以获取更多信息
    猜你喜欢
    • 2018-05-29
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2011-12-19
    • 2017-07-15
    相关资源
    最近更新 更多