没有连接适配器

时间:2017-09-12 20:22:04

标签: android android-recyclerview

我正在为Android创建小应用程序。当我尝试使用带有CardView的RecyclerView时,Android Monitor会说我,而且我的手机上的RecyclerView仍然是空的。而FloatingActionButton也不会与Toast一起工作。也许有些狗屎与背景?因为我在选项卡式活动模板上使用片段。

public class TODOFragment extends Fragment {

RecyclerView recyclerView;
FloatingActionButton addNewTask;
TODOAdapter todoAdapter;

public TODOFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ArrayList<Task> list = new ArrayList<>();
    list.add(new Task("Water","Go to the shop and buy some water"));
    list.add(new Task("Programming","Do test data for recycler view, " +
            "and create for this class"));
    list.add(new Task("Triolan","Go to the shop and buy wifi, lan cable, " +
            "and then make E-net for u self"));

    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_todo, container, false);
    todoAdapter = new TODOAdapter(list);
    recyclerView = (RecyclerView) v.findViewById(R.id.TODOList);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(todoAdapter);
    addNewTask = (FloatingActionButton) v.findViewById(R.id.AddNewTask);
    addNewTask.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), "Added", Toast.LENGTH_LONG).show();
        }
    });
    return v;
}

我的适配器代码

public class TODOAdapter extends RecyclerView.Adapter<TODOAdapter.TaskViewHolder>{

ArrayList<Task> TODO;

public TODOAdapter(ArrayList<Task> TODO) {
    this.TODO = TODO;
}

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

@Override
public void onBindViewHolder(TaskViewHolder holder, int position) {
    holder.taskIcon.setImageResource(R.drawable.mainlogo);
    holder.taskName.setText(TODO.get(position).getTaskName());
    holder.shortTask.setText(TODO.get(position).getShortTask());
}

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

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

public static class TaskViewHolder extends RecyclerView.ViewHolder{
    CardView task;
    TextView taskName;
    TextView shortTask;
    ImageView taskIcon;
    public TaskViewHolder(View itemView) {
        super(itemView);
        task = (CardView)itemView.findViewById(R.id.task_card);
        taskName = (TextView)itemView.findViewById(R.id.TaskName);
        shortTask = (TextView)itemView.findViewById(R.id.ShortTask);
        taskIcon = (ImageView)itemView.findViewById(R.id.TaskIcon);
    }
}

请帮助我0_0

1 个答案:

答案 0 :(得分:0)

正如您可以阅读下一个问题的已接受回复:

Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

您应该只使用覆盖方法onCreateView()来首次实例化UI元素,但是您应该在覆盖中设置RecyclerViewAdapteronClickListener的{​​{1}} FloatingActionButton方法。

希望这对你有所帮助。