简体   繁体   English

更改 recyclerview 项中 textview 的值

[英]change value of textview in a recyclerview item

guys I'm using multiple api request with the same response and displaying it into a recycling view but in those response I get a user_id instead of user name but I need to display the user name into the recyclerview this image breaks down what i need to do : https://i.stack.imgur.com/IdQ6e.png伙计们,我正在使用具有相同响应的多个 api 请求并将其显示到回收视图中,但在这些响应中,我得到一个 user_id 而不是用户名,但我需要将用户名显示到 recyclerview 中,此图像分解了我需要的内容做: https : //i.stack.imgur.com/IdQ6e.png

so this is my adapter class where I tried to make a hashmap using another api request and making a hashmap with <user_id,user_name> and trying to use it in the holder class to display the user name but it didn't work :所以这是我的适配器类,我尝试使用另一个 api 请求制作哈希图,并使用 <user_id,user_name> 制作哈希图,并尝试在持有者类中使用它来显示用户名,但它不起作用:

public class FollowupAdapter extends RecyclerView.Adapter  {

Map<String, String> userMap1 = new HashMap<>();

private Context Context1 ;
private List<TraitementTicketModel> followuplist;




public FollowupAdapter(Context mContext, List<TraitementTicketModel> followuplist) {
    this.Context1 = mContext;
    this.followuplist = followuplist;


}

@Override
public int getItemViewType(int position) {
    if(followuplist.get(position).getTaskcategories_id()!=null) {
        return 3;
    }else if (followuplist.get(position).getSolutiontypes_id()!=null){
        return 2;

    }
    return 1;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;
    LayoutInflater layoutInflater=LayoutInflater.from(Context1);
    if (viewType==1){
        view = layoutInflater.inflate(R.layout.followupitem, parent,false);
        return new ViewHolderFollowup(view);
    }else if (viewType==2){
        view = layoutInflater.inflate(R.layout.solutionitem, parent,false);
        return new ViewHolderSolution(view);
    }
    else
    view = layoutInflater.inflate(R.layout.taskitem, parent,false);
    return new ViewHolderTask(view);
}



@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    displayusers();

    if(followuplist.get(position).getTaskcategories_id()!=null) {
        ViewHolderTask viewHolderTask = (ViewHolderTask) holder;
        viewHolderTask.users_id.setText(userMap1.get(followuplist.get(position).getUsers_id()));
        viewHolderTask.date.setText(followuplist.get(position).getDate());
        viewHolderTask.content.setText(html2text(followuplist.get(position).getContent()));

    }else if(followuplist.get(position).getSolutiontypes_id()!=null){
        ViewHolderSolution viewHolderSolution = (ViewHolderSolution) holder;
        viewHolderSolution.users_id.setText(userMap1.get(followuplist.get(position).getUsers_id()));
        viewHolderSolution.date.setText(followuplist.get(position).getDate());
        viewHolderSolution.content.setText(html2text(followuplist.get(position).getContent()));



    }
    else {
        ViewHolderFollowup viewHolderFollowup = (ViewHolderFollowup) holder;
        viewHolderFollowup.users_id.setText(userMap1.get(followuplist.get(position).getUsers_id()));
        viewHolderFollowup.content.setText(html2text(followuplist.get(position).getContent()));
        viewHolderFollowup.titre.setText(followuplist.get(position).getTitre());
        viewHolderFollowup.date.setText(followuplist.get(position).getDate());
    }


}

@Override
public int getItemCount() { return followuplist.size(); }


 public static class ViewHolderFollowup extends RecyclerView.ViewHolder{

    TextView users_id;
    TextView date;
    TextView content;
    TextView titre;


    public ViewHolderFollowup(@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 class ViewHolderTask extends RecyclerView.ViewHolder {
    TextView users_id;
    TextView date;
    TextView content;



    public ViewHolderTask(@NonNull View itemView) {
        super(itemView);
        users_id=itemView.findViewById(R.id.user_id_task);
        date =itemView.findViewById(R.id.date_task);
        content=itemView.findViewById(R.id.contenu_task);

    }
}

public static class ViewHolderSolution extends RecyclerView.ViewHolder{
    TextView users_id;
    TextView date;
    TextView content;



    public ViewHolderSolution(@NonNull View itemView) {
        super(itemView);
        users_id=itemView.findViewById(R.id.user_id_solution);
        date =itemView.findViewById(R.id.date_solution);
        content=itemView.findViewById(R.id.contenu_solution);

    }

}

    public static String html2text(String html) {
    return Jsoup.parse(Jsoup.parse(html).text()).text();
}
public List addToList(List<TraitementTicketModel> list) {


    if(this.followuplist.addAll(list)){
        Sort(followuplist);
    }
    notifyDataSetChanged();


    return this.followuplist;
}
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;
}
private void displayusers() {
    SharedPreferences sp =Context1.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) {
                if (userMap.get(user.getId()) == null)
                    userMap.put(user.getId(), user.getName());
            }






        }

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) {


        }
    });

}

private Map<String, String> PutMap(HashMap<String, String> userMap) {


    userMap1=userMap;

    return userMap1;
}

} }

this hashmap I tried didn't work appreciate any solution or idea I'm really stuck in this我试过的这个哈希图没有工作,感谢任何解决方案或想法,我真的被困在这个问题上

Your adapter's job is to adapt a data model into a series of view holders, not to transform, combine, fetch, mutate, etc. a different series of data coming from different sources.您的适配器的工作是将数据模型调整为一系列视图持有者,而不是转换、组合、获取、变异等来自不同来源的不同系列数据。

Ask yourself this question: if you could "magically" call an object like this repository.getList() that would return a List of Something and that Something had three fields:问问自己这个问题:如果你能“神奇地”调用一个像这个repository.getList()这样的对象,它会返回一个List of Something并且Something有三个字段:

class Something {
   String id;
   String userName;
   String content;
   String type; //maybe useful for the Adapter to pick what type...
   ... anything else you need
}

So you have a List<Something>() that you can pass to your adapter, wouldn't your adapter suddenly be super simple?所以你有一个List<Something>()可以传递给你的适配器,你的适配器会不会突然变得超级简单?

That's what you need to do;这就是你需要做的; remove all the logic that does not belong in the adapter (I expect only to have a onCreateViewHolder onBindViewHolder getItemViewType and that's it).删除所有属于适配器的逻辑(我希望只有一个onCreateViewHolder onBindViewHolder getItemViewType ,就是这样)。

You're making a big problem into your adapter, which already has a lot of other responsibilities to carry.您正在为您的适配器制造一个大问题,它已经承担了许多其他责任。 This data problem (putting the user + id + content) in a single object is a problem of your data source/repository, and not the adapter of a recycler view .此数据问题(将用户 + id + 内容放在单个对象中)是您的数据源/存储库的问题,而不是回收器视图的适配器

Transform your data before you pass it to your adapter .在将数据传递给适配器之前转换数据 Your life, your fellow developers, and the future you, will be grateful.你的生活、你的开发伙伴以及你的未来,都会心存感激。

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

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