我希望将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) {
}
});
};
答案 0 :(得分:0)
您可以创建两个列表。适配器的一个列表用于备份。当日历"""Simple example that bounces one ball against a floor.
The BallPhysics class defines the "model". The Ball class is the "view".
@author: Victor Norman
"""
from tkinter import *
import pymunk
import pymunk.util
from pymunk import Vec2d
import math, sys, random
class Ball:
RADIUS = 10
def __init__(self, window):
self._window = window
self._window.title("Bouncing Ball with pymunk physics")
self._model = BallPhysics()
self._width = 400
self._canvas = Canvas(self._window, bg='black',
width=self._width, height=self._width)
self._canvas.pack()
self._render()
def _render(self):
self._model.next_step()
x, y = self._model.get_xy_for_ball()
# subtract y values from self._width because y increases from 0 downward.
self._canvas.create_oval(x - self.RADIUS, self._width - (y - self.RADIUS),
x + self.RADIUS, self._width - (y + self.RADIUS),
fill = 'white')
self._canvas.after(20, self._render)
class BallPhysics:
def __init__(self):
self._space = pymunk.Space()
self._space.gravity = (0.0, -900.0)
self._balls = []
mass = 10
inertia = pymunk.moment_for_circle(mass, 0, Ball.RADIUS, (0, 0))
body = pymunk.Body(mass, inertia)
x = random.randint(50, 350)
body.position = x, 400
shape = pymunk.Circle(body, Ball.RADIUS, Vec2d(0,0))
shape.elasticity = 0.9
self._space.add(body, shape)
self._balls.append(shape)
# floor
floor = pymunk.Segment(self._space.static_body, (0.0, 10.0), (400.0, 10.0), 1.0)
floor.friction = 1.0
floor.elasticity = 0.9
self._space.add(floor)
def next_step(self):
# Remove balls that are below the bottom.
balls_to_remove = []
for ball in self._balls:
if ball.body.position.y < 0:
balls_to_remove.append(ball)
for ball in balls_to_remove:
self._space.remove(ball, ball.body)
self._balls.remove(ball)
if len(self._balls) >= 1:
v = self._balls[0].body.position
print("point = %.2f, %.2f" % (v.x, v.y))
self._space.step(1 / 50)
def get_xy_for_ball(self):
ball_num = 0
return (self._balls[ball_num].body.position.x,
self._balls[ball_num].body.position.y)
main = Tk()
app = Ball(main)
main.mainloop()
被解雇时,您可以制作这样的方法
listener