使用Sugar ORM填充RecyclerView

时间:2015-02-04 20:05:37

标签: android android-recyclerview sugarorm

这就是我现在所拥有的正确数量的项目,但它显示了列名称。不是单个的单元名称。例如,我将在数据库中有3个条目(我检查过,它工作正常)。然后,当我尝试填充RecyclerView时,我得到的是3个名称的条目;

我的适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter{

private List<Session> sessionLists;
private static Context sContext;

public RecyclerViewAdapter(List<Session> sessionLists){
    this.sessionLists = sessionLists;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    View layoutView = LayoutInflater
            .from(viewGroup.getContext())
            .inflate(R.layout.item_row, null);
    return new MyViewHolder(layoutView);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    Session sessionList = sessionLists.get(position);
    MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
}

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

/** This is our ViewHolder class */
public static class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView tvSessionName;
    public TextView tvSessionDate;
    public TextView tvArrowsShot;

    public MyViewHolder(View itemView) {
        super(itemView); // Must call super() first
        tvSessionName = (TextView) itemView.findViewById(R.id.tvSessionName);
        tvSessionDate = (TextView) itemView.findViewById(R.id.tvSessionDate);
        tvArrowsShot = (TextView) itemView.findViewById(R.id.tvArrowsShot);
    }
}
}

这是Sugar ORM的Session.java:

public class Session extends SugarRecord<Session> {
String date, name;

public Session(){
}

public Session(String date, String name){
    this.date = date;
    this.name = name;
}

public String[] getSession(){
    return new String[]{date, name};
}

public String toString(){
    return date;
}
}

这就是我在主要活动中调用它的方式:

List<Session> sessionList = Session.listAll(Session.class);
                    RecyclerViewAdapter adapter = new RecyclerViewAdapter(sessionList);
                    recyclerView.setAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

好吧,我没想到忘了设置TextViews。如果你想知道我忘记了

myViewHolder.tvSessionName.setText(sessionLists.get(position).toString());

内的

onBindViewHolder

我相信这是正确的方法。如果没有人通知我。