Firebase数据库更新除了正确的记录外,还会更新错误的记录

时间:2018-09-05 00:29:05

标签: android firebase firebase-realtime-database

我不知道为什么会这样。我有一个recyclerview,列出了一组Hour对象作为带有文本的复选框。

找不到嵌入视频的方法。 YouTube剪辑在这里:https://www.youtube.com/watch?v=KI9FJSfmrXM

电影在左侧显示了我的Firebase RealTime数据库的视图。每个项目对应于我的应用程序右侧的复选框。关键是军事项目中每个项目的小时数。因此,上午8点是8点,下午5点是17点,依此类推。当我选中/取消选中某个项目时,其他项目也会被随机选中/取消选中。我将在该小时内更新整个对象:

    public static class ProtocolRecyclerViewAdapter extends FirebaseRecyclerAdapter<Hour, ProtocolRecyclerViewAdapter.HourHolder> {

    private final DailyScheduleActivity mParentActivity;
    private DatabaseReference mDb;
    private String protocolUserDateKey;
    ProtocolNonMalignant mProtocol = new ProtocolNonMalignant();


    ProtocolRecyclerViewAdapter(DailyScheduleActivity parent, DatabaseReference db, String protocolKey, FirebaseRecyclerOptions<Hour> options) {
        super(options);
        mParentActivity = parent;
        mDb = db;
        protocolUserDateKey = protocolKey;
    }

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

    @Override
    public void onBindViewHolder(@NonNull HourHolder holder, final int position, final Hour currentHour) {

        holder.mHourCheckBox.setText(currentHour.toString());

        String state = determineCheckboxState(currentHour);
        if (state.equals("unchecked")) {
            holder.mHourCheckBox.setChecked(false);
        } else if (state.equals("checked")) {
            holder.mHourCheckBox.setChecked(true);
        }

        //https://stackoverflow.com/questions/25646048/how-to-convert-local-time-to-am-pm-time-format-using-jodatime
        LocalTime time = new LocalTime(currentHour.getMilitaryHour(), 0);
        DateTimeFormatter fmt = DateTimeFormat.forPattern("h:mm a");
        holder.mHour.setText(fmt.print(time));

        holder.mHourCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton cb, boolean isChecked) {

                cb = (CheckBox) cb;

                if (cb.isChecked()) {
                    currentHour.setState(true);
                    mDb.child(protocolUserDateKey).child(String.valueOf(currentHour.getMilitaryHour())).setValue(currentHour);
                } else {
                    currentHour.setState(false);
                    mDb.child(protocolUserDateKey).child(String.valueOf(currentHour.getMilitaryHour())).setValue(currentHour);
                }
            }
        }); 
    }

    class HourHolder extends RecyclerView.ViewHolder {

        final CheckBox mHourCheckBox;
        final ImageView mOpenTasks;
        final TextView mHour;

        HourHolder(View view) {
            super(view);
            mHourCheckBox = (CheckBox) view.findViewById(R.id.cb_hour_all);
            mOpenTasks = (ImageView) view.findViewById(R.id.open_tasks);
            mHour = (TextView) view.findViewById(R.id.hour);
        }
    }

    private String determineCheckboxState(Hour hour) {
        boolean allFalse = true;
        boolean allTrue = true;

        // Check the state of Juice, Meal, and CE.
        if (hour.getJuice() != null && hour.getJuice().getState() == true ||
            hour.getMeal() != null && hour.getMeal().getState() == true ||
            hour.getCe() != null && hour.getCe().getState() == true) {
            allFalse = false;
        } else {
            allTrue = false;
        }

        // Check the state of the supplements.
        ArrayList<Supplement> supplements = hour.getSupplements();
        for (int i = 0; i < supplements.size(); i++) {
            if (supplements.get(i).getState() == true) {
                allFalse = false;
            } else {
                allTrue = false;
            }
        }

        if (allFalse == true) {
            return "unchecked";
        } else if (allTrue == true) {
            return "checked";
        }
    }
}

“。child(String.valueOf(currentHour.getMilitaryHour()))”部分应将更新限制为仅适当的小时,不是吗?当我使用调试器运行所有内容时,“ currentHour”始终是正确的信息。

更新: 此数据库更新代码位于Recyclerview FirebaseRecyclerAdapter的onBindViewHolder中。将Log.d放入onBindViewHolder会在每次随机更新发生时多次记录一条消息。这意味着有时会多次调用onBindViewHolder。但是为什么呢?

0 个答案:

没有答案