大约5个月前,我面临着这个问题,我不再解决了(description)。我尝试了评论中的所有建议,但它们并没有帮助解决我的问题。最初,我认为该错误是由于Firebase引起的,但是最近我创建了另一个应用程序,该应用程序从本地来源而不是从Internet输入数据,并且无论如何都会出现问题。我应该使用ListView类仅加载一次数据吗?从理论上讲,应该解决该问题,但该应用程序将不再表现出色。 下面是新应用的代码。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int MENU_ITEM = 0;
private RecyclerView mEndedList, mUpcomingList;
private AppointmentsAdapter mAdapterEnded, mAdapterUpcoming;
private List<Appointment> listGeneral, appointmentEndedList, appointmentUpcomingList;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//prendiamo la dimensione del display
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
myRef.setValue("Hello, World!");
appointmentUpcomingList = new ArrayList<>();
appointmentEndedList = new ArrayList<>();
listGeneral = new ArrayList<>();
// height - 145(top layout) - (bottom layout) - 10 == (middle layout)
//TODO: finiti (Ended) e in corso (Upcoming) NON VANNO BENE BISOGNA METTERE I FINITI IN UN POSTO PIù NASCOSTO
//TODO: rivedere layout per appunatemnti confermati rifiutati e in corso di conferma
//TODO: quando schiaccio conferma parte l'animazione del riempimento della barra e scompaiono i bottoni
//TODO: arancio da confermare, rosso rifiutato e verde confermato
//TODO: se il promemoria era ieri spariscono i pulsanti
//TODO: c'è solo una lista devo dividere io le due liste in base al giorno
listGeneral.add(new Appointment("title", "dettails", 'i'));
listGeneral.add(new Appointment("title", "dettails", 'c'));
listGeneral.add(new Appointment("title", "dettails", 'i'));
listGeneral.add(new Appointment("title", "dettails", 'c'));
listGeneral.add(new Appointment("title", "dettails", 'r'));
listGeneral.add(new Appointment("title", "dettails", 'i'));
listGeneral.add(new Appointment("title", "dettails", 'c'));
listGeneral.add(new Appointment("title", "dettails", 'i'));
listGeneral.add(new Appointment("title", "dettails", 'i'));
listGeneral.add(new Appointment("title", "dettails", 'r'));
for (Appointment appointment: listGeneral) {
if (appointment.getDateIsPast()) {
appointmentEndedList.add(appointment);
} else {
appointmentUpcomingList.add(appointment);
}
}
//instance
TextView numberAppointments = findViewById(R.id.numberAppointments);
BottomNavigationView navigation = findViewById(R.id.navigation);
mEndedList = findViewById(R.id.appointmentList);
mAdapterEnded = new AppointmentsAdapter(appointmentEndedList, this,
new AppointmentsAdapter.ClickListener() {
@Override
public void onClickListener(int position) {
Intent start = new Intent(MainActivity.this, DettailsAppointment.class);
startActivity(start);
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mEndedList.setLayoutManager(layoutManager);
mEndedList.setAdapter(mAdapterEnded);
mUpcomingList = findViewById(R.id.appointmentUpcomingList);
mAdapterUpcoming = new AppointmentsAdapter(appointmentUpcomingList, this,
new AppointmentsAdapter.ClickListener() {
@Override
public void onClickListener(int position) {
Intent start = new Intent(MainActivity.this, DettailsAppointment.class);
startActivity(start);
}
});
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
mUpcomingList.setLayoutManager(layoutManager2);
mUpcomingList.setAdapter(mAdapterUpcoming);
numberAppointments.setText(String.valueOf(appointmentUpcomingList.size()));
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Menu menu = navigation.getMenu();
MenuItem menuItem = menu.getItem(MENU_ITEM);
menuItem.setChecked(true);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.appointments:
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
return true;
case R.id.message:
startActivity(new Intent(MainActivity.this, MessageActivity.class));
finish();
return true;
}
return false;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Appointment.java
public class Appointment {
private String title;
private String details;
private long date;
private char isConfirmated;
public Appointment(){}
public Appointment(String title, String details, char isConfirmated) {
this.title = title;
this.details = details;
this.date = 1589201486198L;//TODO: System.currentTimeMillis();
this.isConfirmated = isConfirmated;
}
/**
* capire se today o tomorrow
* @return
*/
public String getDate() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
Date appointmentDate = new Date(date);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTimeInMillis(System.currentTimeMillis());
int currentYear = currentCalendar.get(Calendar.YEAR);
int currentMonth = currentCalendar.get(Calendar.MONTH);
int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
if (mDay == currentDay && mMonth == currentMonth && mYear == currentYear) {
DateFormat df = new SimpleDateFormat("HH.mm");
return df.format(appointmentDate) + ", today";
} else if (mDay == currentDay+1 && mMonth == currentMonth && mYear == currentYear) {
DateFormat df = new SimpleDateFormat("HH.mm");
return df.format(appointmentDate) + ", tomorrow";
} else {
DateFormat df = new SimpleDateFormat("HH.mm, dd/MM/yy");
return df.format(appointmentDate);
}
}
/**
* se l'appuntamento è passato
*/
public boolean getDateIsPast() {
/*Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date);
Log.d("Date", String.valueOf(date));
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTimeInMillis(System.currentTimeMillis());
Log.d("Date ora", String.valueOf(System.currentTimeMillis()));*/
long start = System.currentTimeMillis();
long end = date;
long dayRemaind = TimeUnit.MILLISECONDS.toDays(end - start);
if (dayRemaind < 0) {
return true;
}
/*if (calendar.compareTo(currentCalendar) < 0) {
return true;
}*/
return false;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public long getLongDate() {
return date;
}
public char isConfirmated() {
return isConfirmated;
}
/**
* c = confermato, r = rifiutato, i = in attesa
* @param confirmated
*/
public void setConfirmated(char confirmated) {
isConfirmated = confirmated;
}
}
AppointmentsAdapter.java
public class AppointmentsAdapter extends RecyclerView.Adapter<AppointmentsAdapter.MyViewHolder> {
public interface ClickListener {
void onClickListener(int position);
}
private List<Appointment> appointmentsList;
private Context context;
private ClickListener mClickListner;
public AppointmentsAdapter(List<Appointment> appointmentsList, Context context, ClickListener clickListener) {
this.appointmentsList = appointmentsList;
this.context = context;
this.mClickListner = clickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(context).inflate(R.layout.notes_row, viewGroup, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int position) {
Appointment appointment = appointmentsList.get(position);
myViewHolder.title.setText(appointment.getTitle());
myViewHolder.date.setText(appointment.getDate());
if (appointment.getDateIsPast()) {
if (appointment.isConfirmated() == 'c') {
//myViewHolder.isDone.setTextColor(context.getResources().getColor(R.color.Green));
myViewHolder.isDone.setText("");
myViewHolder.btnConfirmated.setVisibility(View.INVISIBLE);
myViewHolder.btnRefused.setVisibility(View.INVISIBLE);
myViewHolder.confermatedText.setVisibility(View.VISIBLE);
myViewHolder.circleImage.setImageDrawable(context.getResources().getDrawable(R.drawable.green_circle));
myViewHolder.progressBarAppoinment.setBackgroundColor(context.getResources().getColor(R.color.Green));
} else if (appointment.isConfirmated() == 'r'){
//myViewHolder.isDone.setTextColor(context.getResources().getColor(R.color.Red));
myViewHolder.isDone.setText("");
myViewHolder.btnConfirmated.setVisibility(View.INVISIBLE);
myViewHolder.btnRefused.setVisibility(View.INVISIBLE);
myViewHolder.refusedText.setVisibility(View.VISIBLE);
myViewHolder.circleImage.setImageDrawable(context.getResources().getDrawable(R.drawable.red_circle));
myViewHolder.progressBarAppoinment.setBackgroundColor(context.getResources().getColor(R.color.Red));
} else {
myViewHolder.isDone.setText("");
myViewHolder.isDone.setTextColor(context.getResources().getColor(R.color.Orange));
myViewHolder.btnConfirmated.setVisibility(View.INVISIBLE);
myViewHolder.btnRefused.setVisibility(View.INVISIBLE);
myViewHolder.pendingConfirmationText.setVisibility(View.VISIBLE);
myViewHolder.circleImage.setImageDrawable(context.getResources().getDrawable(R.drawable.orange_circle));
myViewHolder.progressBarAppoinment.setBackgroundColor(context.getResources().getColor(R.color.Orange));
}
} else {
if (appointment.isConfirmated() == 'c') {
//myViewHolder.isDone.setTextColor(context.getResources().getColor(R.color.Green));
myViewHolder.isDone.setText("");
myViewHolder.btnConfirmated.setVisibility(View.INVISIBLE);
myViewHolder.btnRefused.setVisibility(View.INVISIBLE);
myViewHolder.confermatedText.setVisibility(View.VISIBLE);
myViewHolder.circleImage.setImageDrawable(context.getResources().getDrawable(R.drawable.green_circle));
myViewHolder.progressBarAppoinment.setBackgroundColor(context.getResources().getColor(R.color.Green));
} else if (appointment.isConfirmated() == 'r') {
//myViewHolder.isDone.setTextColor(context.getResources().getColor(R.color.Red));
myViewHolder.isDone.setText("");
myViewHolder.btnConfirmated.setVisibility(View.INVISIBLE);
myViewHolder.btnRefused.setVisibility(View.INVISIBLE);
myViewHolder.refusedText.setVisibility(View.VISIBLE);
myViewHolder.circleImage.setImageDrawable(context.getResources().getDrawable(R.drawable.red_circle));
myViewHolder.progressBarAppoinment.setBackgroundColor(context.getResources().getColor(R.color.Red));
} else {
myViewHolder.isDone.setTextColor(context.getResources().getColor(R.color.Orange));
myViewHolder.isDone.setText("Pending confirmation");
myViewHolder.circleImage.setImageDrawable(context.getResources().getDrawable(R.drawable.orange_circle));
myViewHolder.progressBarAppoinment.setBackgroundColor(context.getResources().getColor(R.color.Orange));
}
}
myViewHolder.btnConfirmated.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
myViewHolder.btnRefused.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
int listSize = appointmentsList.size();
if (position == listSize-1) {
myViewHolder.progressBarAppoinment.setVisibility(View.GONE);
} else {
myViewHolder.progressBarAppoinment.setVisibility(View.VISIBLE);
}
myViewHolder.showMoreInformation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mClickListner != null) {
mClickListner.onClickListener(position);
} else {
throw new IllegalArgumentException("DEVI IMPLEMENTARE L'INTERFACCIA");
}
}
});
}
@Override
public int getItemCount() {
return appointmentsList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView circleImage, showMoreInformation;
private Button btnConfirmated, btnRefused;
private TextView title, date, isDone, confermatedText, refusedText, pendingConfirmationText;
private View progressBarAppoinment;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
btnConfirmated = itemView.findViewById(R.id.confirmationButton);
btnRefused = itemView.findViewById(R.id.refusesButton);
title = itemView.findViewById(R.id.titleText);
date = itemView.findViewById(R.id.dateText);
isDone = itemView.findViewById(R.id.doneText);
progressBarAppoinment = itemView.findViewById(R.id.progressBarAppoinment);
confermatedText = itemView.findViewById(R.id.confirmationText);
refusedText = itemView.findViewById(R.id.refuseText);
circleImage = itemView.findViewById(R.id.circleImage);
pendingConfirmationText = itemView.findViewById(R.id.pendingConfirmationText);
showMoreInformation = itemView.findViewById(R.id.showMoreInformation);
}
}
}