0

I tried to get the documents in subcollection and put it in Arraylist. Then the data in the Arraylist will return in the String method which is the load() method.The load() method must return in String elsewhere the loadPlan() method will be in error. But when I ran the app the return in the load() method is return to null. The data from the firestore is not return to it.How can I solve this. Here is the snapshot of my code and the logcat.

class ViewHolder extends RecyclerView.ViewHolder{
    TextView name;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(getBaseContext(), DJIDashboardActivity.class);
                intent.putExtra("mapCredentialsKey", MAP_CREDENTIALS_KEY);
                startActivityIfNeeded(intent, RequestCodes.DASHBOARD);
                loadPlan();
            }
        });
    }

    public void bind(PlanModel planModel){
        name.setText(planModel.getName());


    }
}

...................

private void loadPlan() {
    try {
        Dronelink.getInstance().loadPlan(load(), false, this, (final String error) -> { Log.e(TAG, "Unable to read mission plan: " + error); });
    } catch (final Dronelink.KernelUnavailableException e) {
        Log.e(TAG, "Dronelink Kernel Unavailable");
    } catch (final Dronelink.UnregisteredException e) {
        Log.e(TAG, "Dronelink SDK Unregistered");
    }
}

 public String load() {


    TempDialog = new ProgressDialog(MainActivity.this);
    TempDialog.setMessage("Please wait ");
    TempDialog.setCancelable(false);
    TempDialog.setProgress(i);
    TempDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    TempDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.GRAY));

    TempDialog.show();



    mission(new FirestoreCallback() {
        @Override
        public void onCallback(List<String> list) {
            Log.d(TAG,"1" + contentList.toString());
        }
    });

    Log.d(TAG, "3 PRINT " + contentList.toString());
    return  contentList.toString();
}

public void mission(FirestoreCallback firestoreCallback) {
    db.collection("plans").document("FQQJY4Ia5xFLKExVkHtl").collection("versions").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {

                        for (QueryDocumentSnapshot doc : task.getResult()) {


                            contentList.clear();

                            String List = doc.getString("content");
                            contentList.add(List);
                            Log.d(TAG, "HARDCODED  "+"㶑ᩙч∣㦣კ䩰㡥ᚚᤘ䜅千⦙岴灦愳ᚰ䷘䚆䌣⧃ᆔ扰∬ᄱ寛湌䚖猋冔䑴笢㨼尙⑇∤㬫㴏ᐰⰢ㘰崚⺎噆⤑棈屪㠵\u1C98์♇፣ᆉ壠晘≬㞷᧱揆ጃᅱ哌牰㤸ᬚ䷍䚇\u0383\u2BE9ら䣊獣㤴尝෮✼嬚ጬⱘ≮ザ焅⧌坲ʃㆅ居䐷㲷ᡛ\u2D2C㒆筫ᔱ廆慴㒷守䝌昖掛ᒰ䓊汥㭢䒜摇▼䪚ᶕ徒⨬ᄰ嬺\u0016䲒\u0A0E⁌塄獹㦺ᥛ⑇∦殛よ塄癡㘺奈䝆挢燁哄恰㌶ᮙ್晆\u17DF砉ϼɁ缁ၲ䁈ྫ勌䊷ဃ璆潭㠷宙ⷎ䈢愓内囊潦㌧妙乬坌寲妕䛨潲ᤑଈ䲍ᜦ⬟᠄㑠Ⱒ㚰姛壭ጇ湉紑勦瑡㜱笀៹ĵ⍻㆕斊ᰬᄴᯜ䴯⛶玣ְ䑴㘮\u181C䶋ю晗Ꭳ▏ࠣ䑏㈹ᯛ䲩囼⍲ㆥ嫒瓬æ猅曄⋂ᎃ㷍厈\u12D7ᆙ䢋я൱朠ص䫘潣㒺Ṳ摙ᇆ欋悈瑨⸴ᮘഋэ嚞 旴塄慣ㆲ礀\u1979睌熱䒸灤㠸ᡥL壨⋓ű壀牬絽ᘑ㌝ῠ७ଇဆ\u05FE\u0091氘彋яⷷ団棌屠㐸掦䭱↿瀈Ὸѓ\u088E〮\u1C98䴍ᥣ⌗漠ʝ廤楥㜺㐕㿠ᷟ砆㟼ʏ缀扥䱿怸᷷囻歽痾ǁ澽\u0B8D朜䁁䆯␅宎娭植㿀Ꮉ㕃হ僔晧甁湨䛟㲠ᷮ堟ᄕ擤潲甂\u087F悇庰⇩䣈山洄ẛ䮍䙏勂᎓㶽槔Ӟᵳ䆩䘆玃㌩嶙䱚ㄷᦲ୍ౌ⍢槉䦙䱚挸ㄱ䴘朌ᙓƃ\u07FC്猅䌘㭁⻜偋矸ᨕ柾ٳᄹᥜ⺭ᜦ⬡ࣩ棤痋ࠣᡚⶎ圦⩫㼑呄摩㦲宙氬繀ѻᗡ䛘畳㒻ᥲ梌晗ጯᰅῊÿ㊚墘䲇ᙣㅩ䃙䙠ⴴ㌙奋ⱆ挦५\u0CE5䱰㤱ᰚౌ䘦繀し᠂度潵㤱奙࣬囵ᮃב勃搆崹煅ч∧᳣ܸ๘䱩㧵䁛斄☶䍋㆓ሣ昇ᱲ䀠暬ᘖᦋჍ䡚㜶ᣲ`䘌䋓䬩壜婨攴㆜\u0DCDⱬ捣䜠\u19AF縃˿Á㿀た瀘៸؋縃˾Áฌ⛆͖ᦳႴ灤㠴ᚚೌ䱥厖↛ҵ䣊搷ᨘ奙䘆♃\u0FF8؋怃˿Ε㉅曉県\u206Bֳࡪ牥㦺\u1C9A㲠糕卻㦕暊湡ㄶ㪀ᶬᜇΓ㶅䛑爃Ⅎഘ✦挶ጙ㖑䩧搁嬛奋ⱆ嘖Ⅹ壔灤㐸㈜䷎ی濰ง簇Ǿǀ羀瀿怸\u0FF0ง簇t∹㊲ᢍ䱆獂榹惁䉚㐶᮱䭎اፂ欫ೠ晪㜲ᢚ塌䘟瀜߸܃縃䃿à㧀ᶸ珴⌫䷓䠂浩㟶䉺䲙ⅿ硏㬕䏈ंᦜ್؆千ƙ擜晢㜹的羍ڜ䂦㆙僤橰㔴ᰜ\u0DCC曜䁣⼸\u2BF5Ⓜ湧稅䕱䌟炳䝐ⳡ䋩欋ᰙ䰋岡݇ᙺ珀፮㔷璃ℱ↼䁧⌋ඡ勊癥㚲宝ઍṀ怱匴⧾િ稃₌◆匣❠ᨋਣ䑬掠吘⺝〴传\u0C71擜敲㒷ᧈ䝄⚖玣ᗉ曊揊\u0C29ᡙമ圾䡘姝䋲灯㓶䅐晧ᘓউ䣀晚㤲ᢙ୍ۦ̂欓ᖙ䉚㐹㈳ᣙ䘦瘓⬙枨ԉ䵾\u2BE3厈䖄⛿砏殘ϴ㘮\u181B\u0DCCܦ玃䧉䓄汰㗭ü䱌春挳ㆱ惀摢㈳\u1C9Aඌ㲀瞟砝析ݲ紬玀㊌♆䍳\u09B1擄婠㕣爁厎ⲅ嘣̱岵䩪愷ᤰ䱘氧َ 䏼ƕ缀整̬䗆㍳㦹僔汢㐵ᤚ䰍暆፮栆㓈桢⸳ᦛഎچ千㇉䣐懈ɢ羀㍻’嘳䦉䶕䉲㤭᪙䱌斦䘓⦩㓠桢搭㌛᥌䛆化↣ә䟾Í羀㍻\u0019厞 \u08C8湤㌴ᨘ䳍⚝瀌憯ᠧቤ㈴\u1C9C不⛟烱乀姵窺絝疁漐淭杆䭳垔ᒗ椅ॳŋ粀㋅Ꭻ㆗ᢗ搎⢩㑈\u1CA1浖殃㆕棊䇧_\u089B䷭晒Ꮸ耂老");
                            Log.d(TAG, "FROM ARRAY "+contentList.get(0));

                        }
                        firestoreCallback.onCallback(contentList);


                    }else {
                        Log.e(TAG, "Firestore error");
                    }
                }

            });
}

private  interface FirestoreCallback{
    void onCallback(List<String> list);
}

4,5

falc0n027
  • 1
  • 1
  • 2
    Please don't post screenshots of your code, or other textual content. Instead post the actual text, and use the formatting tools of Stack Overflow to mark it up. Also see: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – Frank van Puffelen Feb 26 '22 at 15:09
  • 2
    I also recommend reading [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve), as there's not nearly enough information here for us to be able to answer. – Frank van Puffelen Feb 26 '22 at 15:09
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? Please respond with @ – Alex Mamo Feb 27 '22 at 09:36
  • @AlexMamo yes there is error. The error is unable to read mission plan. From the logcat, you can see that the code show "3 PRINT" before 1. 1 is the data from firebase. I want to show the data from firebase first then return it to the String load() method. – falc0n027 Feb 27 '22 at 13:59
  • That's the normal behavior. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Feb 28 '22 at 09:07

0 Answers0