daniel-stoneuk / material-about-library

Makes it easy to create beautiful about screens for your apps
Apache License 2.0
1.12k stars 140 forks source link

Cards not updating with dynamic content #83

Closed csaq7151 closed 6 years ago

csaq7151 commented 6 years ago

Hi,

I´m trying to display dynamic content loaded from a firebase database into one of the cards. However the are showing empty strings where the content of the database should appear.

global variable String phone = "";

then one of my infoCards

MaterialAboutCard.Builder infoCard = new MaterialAboutCard.Builder();
        infoCard.title(info_title);

        infoCard.addItem(ConvenienceBuilder.createPhoneItem(context,
                new IconicsDrawable(context)
                        .icon(GoogleMaterial.Icon.gmd_phone)
                        .color(ContextCompat.getColor(context, colorIcon))
                        .sizeDp(18),
                info_phone,
                true,
                phone));//here I set the string taken from the database

        Log.e("IN-BUILDER","PHONE in materialaboutbuilder:"+phone);

My onCreate

public void onCreate(@Nullable Bundle savedInstanceState) {
        //Get Firebase auth instance
        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        //get database reference
        database = FirebaseDatabase.getInstance().getReference();
        if (user!=null && database!=null) {
            readUserStuff();
        }

        super.onCreate(savedInstanceState);
    }

and the methode readUserStuff

private void readUserStuff(){
        String UID = auth.getCurrentUser().getUid();

        database.child("ServiceUsers").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
                mUser = dataSnapshot.getValue(User.class);
                phone = mUser.getPhone();
                Log.e("READ","PHONE "+mUser.getPhone());
                refreshMaterialAboutList();
            }
//....
}

So the output of the LOGs is as follows.

First the Log with in the getMaterialAboutList:

"IN-BUILDER", ""

= therefore the cards are empty (expected behaviour)

then the methode readUserStuff() has the Log output

"READ" , 34567823456(correct number)

then refreshMaterialAboutList() is called and the Log in the getMaterialAboutList says:

"IN-BUILDER", 34567823456(correct number)

BUT the cards remain empty no matter how often I call refresh. Also recreating the activity has the same result.

Any Idea?

daniel-stoneuk commented 6 years ago

Hi! Thanks for all the detail. Updating global primitive variables will not update the reference in the Material About List, therefore you need to store the ActionItem that you're putting in the list as a global variable and when you get the updated phone number from the database you need to reference the phoneActionItem and call .setSubText(phone) and .setOnClickAction(ConvenienceBuilder.createPhoneOnClickAction(getActivity(), phone) and then refreshMaterialAboutList(). This is a limitation of Java!

csaq7151 commented 6 years ago

Solved it by passing the object from the activity to the fragment. In this case the builder can already use all of the loaded strings. So no need to refresh the List

daniel-stoneuk commented 6 years ago

There are two examples of how to update the items here: https://github.com/daniel-stoneuk/material-about-library/blob/master/app/src/main/java/com/danielstone/materialaboutlibrarydemo/ExampleMaterialAboutFragment.java.

Yeah, sorry I didn't see that message at first. Just wanted to clarify how to use dynamic items if you do need to in the future :)

csaq7151 commented 6 years ago

Thanks for your help. Great library by the way. Exactly what I wanted and everything is working ;)