这就是我现在所拥有的,它获得了正确数量的项目,但它显示了列名。不是单个单元格名称。例如,我将在数据库中有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);

最佳答案

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

myViewHolder.tvSessionName.setText(sessionLists.get(position).toString());内的onBindViewHolder

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

关于android - 使用Sugar ORM填充RecyclerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28330451/

10-10 09:49