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

问题描述 投票:0回答:1

我正在开发一个应用程序,您可以记录您的日常任务并将日期,日期和时间保存到数据库中。您选择日期,日期和时间,并记录包括任务的startTime和endTime,然后将其保存到数据库(FireBase)。尝试检索它时,出现此错误

java.lang.IndexOutOfBoundsException:索引:1,大小:1

如果我发表评论

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

且仅可离开

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

将检索日期,否则我将收到错误:

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

    }
}`

我希望能够检索每个日期及其下面的预期任务。

这是我的数据库结构

enter image description here

java firebase android-recyclerview adapter
1个回答
0
投票

我通常在onBindViewholder中进行设置

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

使用viewPager和片段将其设置为按索引获取任务并设置到相同位置

  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));
}
© www.soinside.com 2019 - 2024. All rights reserved.