Я создал recyclerView, который содержит несколько cardViews. Проблема, с которой я сталкиваюсь - это динамическое изменение данных на основе информации из базы данных. В настоящее время у меня есть число, хранящееся в firebase , на которое я хотел бы повлиять, какие cardViews в конечном итоге будут установлены на View.GONE или просто обновят текст в textView.
Пока что я не нашел в Интернете ничего относящегося к этой проблеме.
Сейчас я пытаюсь:
@Override
public void onBindViewHolder(final exerciseViewHolder holder, final int i) {
String s1 = exerciseList.get(i);
//Possible error if the name contains "Amount"
final String name = s1.substring(0, s1.indexOf("Amount")-1); //first section of the string to give the name.
String s2 = s1.substring(s1.indexOf(": ")); //cuts of the front of the string and puts to a new substring.
String a = s2.substring(s2.indexOf(' '), s2.indexOf("\n")); //cuts the part after the : but before the \n
a = a.trim(); //trims the inevitable white space
final String units = a.substring(a.indexOf(' ')); //gets the units off the string
a = a.substring(0, a.indexOf(' ')); //cuts off the units
final int amount = Integer.parseInt(a); //parse to use as a number
String s3 = s2.substring(s2.indexOf("\n")); //cuts to the date and new string
s3 = s3.substring(8); //removes the date tag
String y = s3.substring(0, s3.indexOf('/')); //cuts year into new string
final int year = Integer.parseInt(y); //parse year to use as a number
s3 = s3.substring(s3.indexOf('/') + 1); //cuts year off
String d = s3.substring(0, s3.indexOf('/')); //cuts day into new string
final int day = Integer.parseInt(d); //parse day
s3 = s3.substring(s3.indexOf('/') + 1); //cuts day off
final int month = Integer.parseInt(s3); //parses month
DatabaseReference databaseReferenceAthlete = FirebaseDatabase.getInstance().getReference("Athlete Users");
databaseReferenceAthlete.addValueEventListener(new ValueEventListener() {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
final String uid = user.getUid();
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataSnapshot snapshot = dataSnapshot.child(uid).child("currChallenges").child(ChallengeView.cName).child(name);
try{
int val = ((int)((long)snapshot.getValue()));
int printVal = Math.max(0, amount - val);
holder.amount.setText(printVal + units);
holder.exerciseName.setText(name);
}
catch(Exception e){
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
В настоящий момент, я думаю, что recyclerView вызывает какой-то метод finalize / recycle перед выполнением ссылки на базу данных.