0

I'm creating little app for android. when i try to use RecyclerView with CardView, Android Monitor say me his, and on my phone RecyclerView still empty. And FloatingActionButton don't work too, with Toast. Maybe some shit with context? Cause i'm using fragments on tabbed activity template.

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;
}

And my adapter code

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);
    }
}

Help me pls 0_0

1 Answers1

0

As you could read in the accepted response of the next question:

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

You should only use the override method onCreateView() for first instantiation of the UI elements, but you should set the RecyclerViewAdapter and the onClickListener of the FloatingActionButton in the override of the onActivityCreated() method.

Hope this help you.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
mr90
  • 54
  • 1
  • 4