gpt4 book ai didi

android - 如何根据日期和时间对我的 RecyclerView 进行排序

转载 作者:行者123 更新时间:2023-11-30 00:46:26 24 4
gpt4 key购买 nike

如图所示,我的列表没有按照日期和时间排序

回收站查看图片 enter image description here 这是我的 firebase 结构

Firebase 图片 enter image description here

Add Appointment.java -> this is how i add my data to firebase

  public class AddAppointment extends AppCompatActivity {

private FirebaseAuth firebaseAuth;
private FirebaseUser mCurrentUser;


private EditText dateApp,timeApp,nameTxt,addressTxt,telTxt;
int mYear,mMonth,mDay,mHour,mMinute;
private Button saveBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_appointment);
Firebase.setAndroidContext(this);


nameTxt = (EditText) findViewById(R.id.nameText);
addressTxt= (EditText) findViewById(R.id.addressText);
telTxt = (EditText) findViewById(R.id.telText);
dateApp = (EditText)findViewById(R.id.dateAppointment);
timeApp = (EditText)findViewById(R.id.timeAppointment);
dateApp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePic();
}
});
timeApp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timePic();
}
});
saveBtn = (Button)findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseAuth = FirebaseAuth.getInstance();
mCurrentUser = firebaseAuth.getCurrentUser();
DatabaseReference db= FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment").push();
String name = nameTxt.getText().toString().trim();
String address = addressTxt.getText().toString().trim();
String tel = telTxt.getText().toString().trim();
String date = dateApp.getText().toString().trim();
String time = timeApp.getText().toString().trim();

//Adding values
db.child("name").setValue(name);
db.child("address").setValue(address);
db.child("tel").setValue(tel);
db.child("date").setValue(date);
db.child("time").setValue(time);

//Storing values to firebase
Toast.makeText(AddAppointment.this,"Saved",Toast.LENGTH_SHORT).show();
Intent mainIntent = new Intent(AddAppointment.this,MainActivity.class);
startActivity(mainIntent);
}
});
}
public void datePic()
{
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
mYear = year;
mMonth = monthOfYear+1;
mDay = dayOfMonth;
dateApp.setText((mDay+"-"+mMonth+"-"+mYear));
}
},mDay,mMonth,mYear);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
public void timePic()
{
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
int hour = hourOfDay % 12;
timeApp.setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
minute, hourOfDay < 12 ? "a.m." : "p.m."));
}
},mHour,mMinute,false);
timePickerDialog.show();
}
}

AppointmentData.java -> this is my data setup

    public class AppointmentData {

private String name,tel,address,time,date;

public AppointmentData() {
}

public void setName(String name) {
this.name = name;
}

public void setTel(String tel) {
this.tel = tel;
}

public void setAddress(String address) {
this.address = address;
}

public void setTime(String time) {
this.time = time;
}

public void setDate(String date) {
this.date = date;
}

public AppointmentData(String name, String tel, String address, String time, String date) {
this.name=name;
this.tel=tel;
this.address=address;
this.time=time;
this.date=date;
}

public String getName() {
return name;
}

public String getTel() {
return tel;
}

public String getAddress() {
return address;
}

public String getDate() {
return date;
}

public String getTime() {
return time;
}

}

ViewAppointment.java -> this is how i display my recyclerview

 public class ViewAppointment extends AppCompatActivity {

private FirebaseAuth firebaseAuth;
private FirebaseUser mCurrentUser;
private DatabaseReference db;
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter<AppointmentData,myViewHolder> mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_appointment);


recyclerView=(RecyclerView)findViewById(R.id.recycle);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
firebaseAuth = FirebaseAuth.getInstance();

mCurrentUser = firebaseAuth.getCurrentUser();
db= FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment");
db.keepSynced(true);
mAdapter= new FirebaseRecyclerAdapter<AppointmentData, myViewHolder>(

AppointmentData.class,R.layout.model,myViewHolder.class,db
) {


@Override
protected void populateViewHolder(final myViewHolder viewHolder, final AppointmentData model, final int position) {
viewHolder.nameTxt.setText(model.getName());
viewHolder.dateApp.setText(model.getDate());
viewHolder.timeApp.setText(model.getTime());
viewHolder.mapBtn.setText("Map");
viewHolder.detailBtn.setText("Detail");
viewHolder.deleteBtn.setText("Delete");
viewHolder.editBtn.setText("Edit");
Log.e("myFirebase UID", mAdapter.getRef(position).getKey());
viewHolder.editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (view.getContext(), UpdateAppointment.class);
i.putExtra("NAME_KEY",model.getName());
i.putExtra("ADDRESS_KEY",model.getAddress());
i.putExtra("TEL_KEY",model.getTel());
i.putExtra("DATE_KEY",model.getDate());
i.putExtra("TIME_KEY",model.getTime());
view.getContext().startActivity(i);
Log.e("Item Click position",""+position);
mAdapter.getRef(+position).removeValue();
}
});
viewHolder.detailBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (view.getContext(), DetailActivity.class);
i.putExtra("NAME_KEY",model.getName());
i.putExtra("ADDRESS_KEY",model.getAddress());
i.putExtra("TEL_KEY",model.getTel());
i.putExtra("DATE_KEY",model.getDate());
i.putExtra("TIME_KEY",model.getTime());
view.getContext().startActivity(i);
}
});
viewHolder.mapBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (view.getContext(), Distance.class);
i.putExtra("ADDRESS_KEY",model.getAddress());
view.getContext().startActivity(i);
}
});
viewHolder.deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Item Click position",""+position);
mAdapter.getRef(+position).removeValue();
}
});
}
};
recyclerView.setAdapter(mAdapter);

}
public static class myViewHolder extends RecyclerView.ViewHolder{
private TextView nameTxt,dateApp,timeApp;
private Button detailBtn,mapBtn,deleteBtn,editBtn;

public myViewHolder(View itemView){
super(itemView);
nameTxt=(TextView)itemView.findViewById(R.id.nameTxt);
dateApp= (TextView) itemView.findViewById(R.id.dateAppointment);
timeApp=(TextView)itemView.findViewById(R.id.timeAppointment);
mapBtn = (Button) itemView.findViewById(R.id.mapBtn);
detailBtn = (Button) itemView.findViewById(R.id.detailBtn);
deleteBtn = (Button) itemView.findViewById(R.id.deleteBtn);
editBtn = (Button) itemView.findViewById(R.id.editBtn);
}
}


}

最佳答案

Change 对您的 AppointmentData 进行以下更改

public class AppointmentData {
// ...
private Object date;
// ...
public void setDate(Object date) {
this.date = date;
}
// ...
public AppointmentData(String name, String tel, String address, String time, Object date) {
// ...
}
// ...
public Object getDate() {
return date;
}
}

将数据库更改为:

Query db = FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment").orderByChild("date");

将 populateViewHolder 中的这一行更改为:viewHolder.dateApp.setText(timestampToDateString(model.getDate()));

其中 timestampToDateString 是一个实用方法:

public static String timestampToDateString(long timestamp){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date(timestamp);
return dateFormat.format(date);
}

将 btnSave.onClickListener 改成这个

    final String key = FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment").push().getKey();

// ...
Long date = mSelectedDate;
// ...
AppointmentData appointmentData = new AppointmentData(name, tel, address, time, date);

FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment").child(key).setValue(appointmentData);
// ...

像这样添加字段

private long mSelectedDate;

像这样在 datePic() 中初始化它

    // ...
mYear = c.get(Calendar.YEAR);
// ...
mSelectedDate = fieldToTimestamp(mYear, mMonth, mDay);

并在 onDateSelectedListener.onDateSet(...) 中像这样

// ...
mSelectedDate = fieldToTimestamp(year, monthOfYear, dayOfMonth);

其中 fieldToTimestamp 是辅助方法

private long fieldToTimestamp(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
return (long) (calendar.getTimeInMillis() / 1000L);
}

我想就这些了..哦,删除你所有的旧约会,这样就不会导致异常。

关于android - 如何根据日期和时间对我的 RecyclerView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41716185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com