如何将数组放入RecyclerView的项目中?

时间:2018-05-14 20:58:40

标签: java android android-recyclerview

我想在RecyclerView的第一项中添加一组名称(NomUser)。所以我知道我需要更改构造函数的字符串,放置String [],但是当我做一个新的PendingTrajectPOJO时我怎么能把这些信息放进去?

I want the names of Joan and Ousmane on the first item

Like this

这是我放置信息的地方

 listPendingTraject.add(new PendingTrajectPOJO(nomUser,"Surname","Origin","Destination",Data));

    adapter = new AdapterPendingTraject(listPendingTraject);
    recyclerPendingTraject.setAdapter(adapter);

这是我的构造函数

public PendingTrajectPOJO(String name, String surName, String origin, String destination, String date) {
    Name = name;
    SurName = surName;
    Origin = origin;
    Destination = destination;
    Date = date;
}

我的适配器

public class AdapterPendingTraject extends RecyclerView.Adapter<AdapterPendingTraject.ViewHolderPendingTraject>{

ArrayList<PendingTrajectPOJO> listPendingTraject;

public AdapterPendingTraject(ArrayList<PendingTrajectPOJO> listPendingTraject) {
    this.listPendingTraject = listPendingTraject;
}

@Override
public ViewHolderPendingTraject onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.pending_traject_item,null,false);
    return new ViewHolderPendingTraject(view);
}

@Override
public void onBindViewHolder(ViewHolderPendingTraject holder, int position) {
    holder.Name.setText(listPendingTraject.get(position).getName());
    holder.Surname.setText(listPendingTraject.get(position).getSurName());
    holder.Origin.setText(listPendingTraject.get(position).getOrigin());
    holder.Destination.setText(listPendingTraject.get(position).getDestination());
    holder.Date.setText(listPendingTraject.get(position).getDate());

    holder.itemView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            Intent intent = new Intent(context, Journey.class);
            //intent.putExtra("FileName", list.get(position));
            context.startActivity(intent);
        }
    });
}

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

public class ViewHolderPendingTraject extends RecyclerView.ViewHolder {

    TextView Name,Surname,Origin,Destination,Date;
    public ViewHolderPendingTraject(View itemView) {
        super(itemView);
        Name=(TextView)itemView.findViewById(R.id.tvptName);
        Surname=(TextView)itemView.findViewById(R.id.tvptSurName);
        Origin=(TextView)itemView.findViewById(R.id.tvptOrigin);
        Destination=(TextView)itemView.findViewById(R.id.tvptDestination);
        Date=(TextView)itemView.findViewById(R.id.tvptDate);
    }

}

}

2 个答案:

答案 0 :(得分:1)

您要做的是在适配器中创建一个公共方法以传入新项目。在此方法中,您需要通知适配器您已更新项目。我建议使用DiffUtil找出哪些项目已经改变如下:

public void addNewItem(PendingTrajectPOJO p) {
    ArrayList<PendingTrajectPOJO> old = new ArrayList<PendingTrajectPOJO>(listPendingTraject)

    this.listPendingTraject = new ArrayList<PendingTrajectPOJO>()
    this.listPendingTraject.add(p)
    this.listPendingTraject.addAll(old)

    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffCallback(old, this.listPendingTraject));
    diffResult.dispatchUpdatesTo(this);
}

这样,当您添加项目时,它会为它们设置动画并将旧项目向下滑动(因为您提到了您希望顶部项目成为新项目)

注意:我必须稍后对此进行测试,我是这样写的

答案 1 :(得分:0)

在适配器中,创建一个接受PendingTrajectPOJO作为参数的自定义方法,然后更新listPendingTraject

示例:

public void addNewItem(PendingTrajectPOJO p) {
    listPendingTraject.add(p);
    notifyDataSetChanged();
}