我正在创建一个简单的程序,用于每天保存值和时间戳

java - 将数据传递到查询中,而不更新-LMLPHP

保存后,我想为“回收者”视图选择一个日期,以显示与其相关的所有值

无论如何,每次按下按钮都会使passDate更新吗?

我意识到这可能是一个非常简单的解决方法,但我已经陷入了困境。

我曾尝试将查询从onCreate中删除,但遇到了麻烦,并且无法将适配器从oncreate中删除,而不会遇到重大问题

最佳答案

DatePickerDialog.OnDateSetListener异步运行,因此选择日期不会阻止应用程序的其余部分。这意味着用户选择的日期仅在onDateSet中可用。所有需要日期的代码都必须在onDateSet之内或从那里调用。

因此,最简单的解决方案是将所有与日期有关的代码移到onDateSet中:

mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        month = month + 1;
        String yearAsString = Integer.toString(year);
        String monthAsString = Integer.toString(month);
        String dayAsString = Integer.toString(day);
        if (day <= 9 & month <= 9) {
            String date = "0" + dayAsString + "-" + "0" + monthAsString + "-" + yearAsString;
            mDisplayDate.setText(date);
        } else if (day <= 9 & month > 9) {
            String date = "0" + dayAsString + "-" + monthAsString + "-" + yearAsString;
            mDisplayDate.setText(date);
        } else if (month <= 9) {
            String date = dayAsString + "-" + "0" + monthAsString + "-" + yearAsString;
            mDisplayDate.setText(date);
        } else {
            String date = dayAsString + "-" + monthAsString + "-" + yearAsString;
            mDisplayDate.setText(date);
        }

        String passDate = mDisplayDate.getText().toString().trim();

        Log.i(TAG, "PassDate = " + passDate);

        Query query = databaseReference.orderByChild("timestamp").equalTo(passDate);
        options = new FirebaseRecyclerOptions.Builder<Records>().setQuery(query, Records.class).build();

        adapter = new FirebaseRecyclerAdapter<Records, RecordsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull RecordsViewHolder holder, int position, @NonNull Records model) {
                holder.t1.setText(model.getTimestamp());

            }

            @NonNull
            @Override
            public RecordsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.saved_records_view, viewGroup, false);

                return new RecordsViewHolder(view);
            }
        };
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
    }
};


您还可以使视图类实现DatePickerDialog.OnDateSetListener,以减少缩进量:

public class SavedFeedRecords extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {


这意味着onDateSet成为SavedFeedRecords上的常规方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saved_feed_records);
    mDisplayDate = findViewById(R.id.tvDate);
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);

    databaseReference = FirebaseDatabase.getInstance().getReference().child(fAuth.getCurrentUser().getUid()).child("feedRecords");


    findViewById(R.id.tvDate).setOnClickListener(displayDate);
    findViewById(R.id.button).setOnClickListener(viewRecords);
};

public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    month = month + 1;
    String yearAsString = Integer.toString(year);
    String monthAsString = Integer.toString(month);
    String dayAsString = Integer.toString(day);
    if (day <= 9 & month <= 9) {
        String date = "0" + dayAsString + "-" + "0" + monthAsString + "-" + yearAsString;
        mDisplayDate.setText(date);
    } else if (day <= 9 & month > 9) {
        String date = "0" + dayAsString + "-" + monthAsString + "-" + yearAsString;
        mDisplayDate.setText(date);
    } else if (month <= 9) {
        String date = dayAsString + "-" + "0" + monthAsString + "-" + yearAsString;
        mDisplayDate.setText(date);
    } else {
        String date = dayAsString + "-" + monthAsString + "-" + yearAsString;
        mDisplayDate.setText(date);
    }
    String passDate = mDisplayDate.getText().toString().trim();

    Log.i(TAG, "PassDate = " + passDate);

    Query query = databaseReference.orderByChild("timestamp").equalTo(passDate);
    options = new FirebaseRecyclerOptions.Builder<Records>().setQuery(query, Records.class).build();

    adapter = new FirebaseRecyclerAdapter<Records, RecordsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull RecordsViewHolder holder, int position, @NonNull Records model) {
            holder.t1.setText(model.getTimestamp());

        }

        @NonNull
        @Override
        public RecordsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.saved_records_view, viewGroup, false);

            return new RecordsViewHolder(view);
        }
    };
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
    recyclerView.setLayoutManager(gridLayoutManager);
    recyclerView.setAdapter(adapter);
}

07-28 01:19