简体   繁体   English

如何从 Firebase 一起检索字符串和列表

[英]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).您 select 日期、日期和时间,还记录 startTime 和 endTime 包括任务然后保存到数据库(FireBase)。 When am trying to retrieve it I get this error当我试图检索它时,我收到了这个错误

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 java.lang.IndexOutOfBoundsException:索引:1,大小: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我在 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使用 viewPager 和片段我将其设置为按索引获取任务并设置为等效的 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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