简体   繁体   中英

How to retrieve a String and a List together from Firebase

I am working on an app that you can record your daily tasks and save to database with the day, date and time. You select the day, date and time and also record the startTime and endTime including the tasks then save to database(FireBase). When am trying to retrieve it I get this error

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

If I comment

holder.startTime.setText(data.getTasks().get(0).getStartTime());
       holder.endTime.setText( data.getTasks().get(1).getEndTime());
        holder.taskTitle.setText(data.getTasks().get(2).getTask());

and leave only

holder.date.setText(data.getDate());

the date will be retrieved else I get the error:

    public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MainViewHolder> {
    private List<TimeTableData> tableDataList= new ArrayList<>();
    //private List<TimeTableData.Task> taskList = new ArrayList<>();

    public void setTaskData(List<TimeTableData> tableDataList) {
     this.tableDataList.addAll(tableDataList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_item_layout, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        TimeTableData data = tableDataList.get(position);
        holder.date.setText(data.getDate());
       holder.startTime.setText(data.getTasks().get(0).getStartTime());
       holder.endTime.setText( data.getTasks().get(1).getEndTime());
        holder.taskTitle.setText(data.getTasks().get(2).getTask());

    }

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

    public class MainViewHolder extends RecyclerView.ViewHolder {
        public TextView startTime, endTime, taskTitle,date;

        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            startTime = itemView.findViewById(R.id.start_time);
            endTime = itemView.findViewById(R.id.end_time);
            taskTitle = itemView.findViewById(R.id.task_title);
            date = itemView.findViewById(R.id.date);
        }
    }
}

  recyclerView = findViewById(R.id.rv_task);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mainAdapter = new TaskAdapter();

         mFirebaseDatabase=FirebaseDatabase.getInstance();
        mDataBaseReference =mFirebaseDatabase.getReference().child("timeTable");

        mChildEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                List<TimeTableData> taskLis = new ArrayList<>();
              TimeTableData subTask  = dataSnapshot.getValue(TimeTableData.class);
              taskLis.add(subTask);
                mainAdapter.setTaskData(taskLis);
                recyclerView.setAdapter(mainAdapter);
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
        mDataBaseReference.addChildEventListener(mChildEventListener);`

    public class TimeTableData implements Serializable {
    private String id;
    private String date;
   private  List<Task> tasks;

public TimeTableData(){}
    public TimeTableData(  String date,List<Task> taskList) {
        //this.setId(id);
        this.setDate(date);
        this.setTasks(taskList);

    }

//   public TimeTableData(long date){
//       this.setDate(date);
//   }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }


    public static class Task {
       private   String task;
        private String startTime;
        private String endTime;



        public String getEndTime() {
            return endTime;
        }

        public void setEndTime(String endTime) {
            this.endTime = endTime;
        }


        public String getTask() {
            return task;
        }

        public  void setTask(String task) {
            this.task = task;
        }

        public String getStartTime() {
            return startTime;
        }

        public void setStartTime(String startTime) {
            this.startTime = startTime;
        }

    }
}`

I want to be able to retrieve each date with the expected tasks under it.

This is my database structure

在此处输入图像描述

I set it up normally in the onBindViewholder

 holder.startTime.setText(data.getStartTime());
                holder.endTime.setText(data.getEndTime());
                holder.taskTitle.setText(data.getTask());

Using viewPager and fragment I set it up to get tasks by index and set to equivalent position

  public static Fragment newInstance(TimeTableData timeTableData) {
    AllTasksFragment fragment = new AllTasksFragment();
    Bundle bundle = new Bundle();
    bundle.putParcelable(TABLE_DATA, timeTableData);
    fragment.setArguments(bundle);
    return fragment;
}
  @NonNull
@Override
public Fragment getItem(int position) {
return AllTasksFragment.newInstance(tableDataList.get(position));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM