0

I have created a recyclerView that holds multiple cardViews. The problem I am having is dynamically changing the data based on information from firebase. I currently have a number stored in firebase that I would like to influence which cardViews end up being set to View.GONE or to just update the text in a textView.

So far I have found nothing relevant on the internet pertaining to this problem.

Currently what I am trying:

@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) {

            }
        });



    }

At the moment, I think that the recyclerView calls some sort of finalize/recycle method before the database reference runs.

Happy Singh
  • 1,334
  • 9
  • 23
Merthew
  • 61
  • 5
  • If you're building your own adapter, you can control the layout it uses for each item by overriding `getItemViewType()`: https://stackoverflow.com/q/26245139. If you're using the adapter from FirebaseUI, see: https://stackoverflow.com/a/39082735 – Frank van Puffelen Apr 10 '19 at 00:45
  • At the moment, all I want to do is edit the text in a TextView – Merthew Apr 10 '19 at 01:35

0 Answers0