【问题标题】:How to refresh listView date on CalenderView date changed如何在 CalenderView 日期更改时刷新 listView 日期
【发布时间】:2017-11-15 17:23:47
【问题描述】:

我想将我的listView 与我的CalenderView 同步,以便在更改日历上的新日期时,仅显示日期与日历视图上的日期相同的约会。我正在使用 Firebase 实时数据库,我认为让它工作的唯一方法是每次进行选择时更新数据库,但我认为这不是最佳/正确的方法。如果需要任何其他课程,我已经附上了下面的课程,我也可以添加它们。为了清楚起见,我还附上了屏幕图像。提前致谢

package com.example.steph.fyp;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class BookAppointment extends AppCompatActivity {

ListView listViewAppointments;
DatabaseReference databaseAppointment;

List<Appointment> appointmentList;
DateHelper dateHelper = new DateHelper();
CalendarView calendarView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book_appointment);

    listViewAppointments = (ListView) findViewById(R.id.listview_appointments);
    calendarView = (CalendarView) findViewById(R.id.calendar_view);

    //getting the reference of appointments node
    databaseAppointment = FirebaseDatabase.getInstance().getReference("appointments");
    appointmentList = new ArrayList<>();



    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

        }
    });
    listViewAppointments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //getting the selected appointment
            Appointment appointment = appointmentList.get(i);
            showUpdateDialog(appointment.getId(), appointment.getDate());
        }
    });

}

private boolean updateAppointmentBooked(String id, long date) {
    //getting the specified appointments reference
    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("appointments").child(id);

    //updating appointment
    Appointment appointment = new Appointment(id, date, true);
    dR.setValue(appointment);
    Toast.makeText(getApplicationContext(), "Booking Successful", Toast.LENGTH_LONG).show();
    return true;
}
private void showUpdateDialog(final String appointmentId, final long appointmentDate){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);


    LayoutInflater inflater = getLayoutInflater();

    final View dialogView = inflater.inflate(R.layout.book_appointment_dialog, null);

    dialogBuilder.setView(dialogView);

    final TextView textViewDate = (TextView) dialogView.findViewById(R.id.textview_date);
    final TextView textViewTime = (TextView) dialogView.findViewById(R.id.textview_time);
    final TextView textViewCancel = (TextView) dialogView.findViewById(R.id.textview_cancel);
    final Button bookAppointmentButton = (Button) dialogView.findViewById(R.id.book_appointment_button);

    textViewDate.setText(dateHelper.toAppointmentDate(appointmentDate));
    textViewTime.setText(dateHelper.toAppointmentTime(appointmentDate));
    dialogBuilder.setTitle("Confirm Booking");

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();



    bookAppointmentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            updateAppointmentBooked(appointmentId,appointmentDate);
            alertDialog.dismiss();
        }

    });
    textViewCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }

    });
    //alertDialog.dismiss();




}

@Override
protected void onStart() {
    super.onStart();
    //attaching value event listener
    databaseAppointment.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //clearing the previous artist list
            appointmentList.clear();

            //iterating through all the nodes
            for (DataSnapshot appointmentSnapshot : dataSnapshot.getChildren()) {
                //getting artist
                Appointment appointment = appointmentSnapshot.getValue(Appointment.class);
                if(!appointment.isBooked() && dateHelper.isDateSameDay(calendarView.getDate(), appointment.getDate())){
                    //adding appointment to the list
                    appointmentList.add(appointment);
                }

            }

            //creating adapter
            AppointmentList appointmentAdapter = new AppointmentList(BookAppointment.this, appointmentList);

            //attaching adapter to the listview
            //expandableListViewAppointments.setAdapter(expandableListViewAdapter);
            listViewAppointments.setAdapter(appointmentAdapter);

        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });
    };

【问题讨论】:

    标签: java android firebase listview firebase-realtime-database


    【解决方案1】:

    您可以创建两个列表。一个用于适配器的列表另一个用于备份。当日历listener 被触发时,您可以制作这样的方法

    addList(){ 
    list1.clear(); // main list clear
    list1.addAll(list2) // backup list added
    }
    

    【讨论】:

    • 我将如何获得第二个列表的项目?我当时需要一张“桌子”的快照,但我不知道如何访问它
    • 当你得到第一个列表的时候你可以存储第二个列表
    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2021-03-16
    • 2021-02-22
    相关资源
    最近更新 更多