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

时间:2019-02-05 14:08:37

标签: java android firebase firebase-realtime-database

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

Firebase

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

每按一次按钮,是否有要获取passDate更新的信息?

我意识到这可能是一个非常简单的解决方法,但我还是陷入了循环。

我尝试将查询从onCreate中删除,但是遇到了麻烦,并且在遇到重大问题的情况下我无法将适配器从oncreate中删除

1 个答案:

答案 0 :(得分:1)

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