RecyclerView onBind,不绑定数据

时间:2017-10-13 15:51:54

标签: java android android-recyclerview

我在这里遇到问题。在绑定数据时,不能使用公共对象。我应该创建一个从其他类获取数据并将文本设置为某些XML的类。

public class EventsAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<LauncherActivity.ListItem> listItems;
private Context context;
public EventsAdapter(List<LauncherActivity.ListItem> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_list, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    LauncherActivity.ListItem listItem = listItems.get(position);
    holder.
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView textHeading = null;
    public TextView textDescription = null;
    public ViewHolder(View itemView) {
        super(itemView);
        textHeading = (TextView)itemView.findViewById(R.id.viewHead);
        textDescription = (TextView)itemView.findViewById(R.id.viewDesc);
    }
}

}

5 个答案:

答案 0 :(得分:0)

由于您在onBind方法中使用RecyclerView.ViewHolder,因此无法解析。使用自定义EventsAdapter.ViewHolder类来解析变量。例如:

public void onBindViewHolder(EventsAdapter.ViewHolder holder, int position) {

}

答案 1 :(得分:0)

尝试更改为此扩展..     RecyclerView.Adapter&LT; “EventsAdapter.ViewHolder” &GT;没有“”当然

答案 2 :(得分:0)

使您的EventsAdapter扩展RecyclerView.Adapter<EventsAdapter.MyViewholder>,然后再次实施方法和构造函数,这样您的onBindViewHolder将会自己创建EventsAdapter.ViewHolder holder

希望这会有所帮助..

答案 3 :(得分:0)

您没有使用ViewHolder,这就是无法使用这些方法的原因

改变这个:

public class EventsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>

对此:

public class EventsAdapter extends RecyclerView.Adapter<ViewHolder> //your ViewHolder

你的onBindViewHolder会是这样的

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    LauncherActivity.ListItem listItem = listItems.get(position);

}

答案 4 :(得分:0)

有两种方法可以做到这一点:

使您的EventsAdapter扩展RecyclerView.Adapter

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

   LauncherActivity.ListItem listItem = listItems.get(position);
   holder.textHeading.setText(....)
}

在类型转换的帮助下

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

   LauncherActivity.ListItem listItem = listItems.get(position);

     ((ViewHolder)holder).textHeading.setText(....)
}

你很高兴。 :)快乐编码:)