简体   繁体   中英

Firebase get all child values and store in an array

I need to get all child "answer" values from the database into an array.

Afterwards, I want to match that array with another array. I am new to coding, i require help. My Database is linked here. I tried it with my code below, but I think my logic is wrong and I cant match the stringArray with the ProductArray.

public class Activity extends AppCompatActivity {

    private String[] stringArray = new String[3];
    private String[] ProductArray = new String[]{"traveling","short","not much"};

    private TextView productTwo;
    private DatabaseReference mRef;

    private int mQuestionNumber = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommendations);

        productTwo = (TextView) findViewById(R.id.recommendations_product2);

        mRef = FirebaseDatabase.getInstance().getReference().child("BTSpeakers").child(String.valueOf(mQuestionNumber));
        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if(dataSnapshot.exists())
                {
                    String Answer = dataSnapshot.child("answer").getValue().toString();

                    //Store Answer values of Database in Array
                    stringArray[mQuestionNumber] = Answer;
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
        mQuestionNumber++;

        //If array values equals ProductArray then , setText To Product2
        if(ProductArray.equals(stringArray))
        {
            productTwo.setText("Speakers");
        }
    }
}

It is more efficient if you use list instead of array, in your case you can do the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recommendations);

    productTwo = (TextView) findViewById(R.id.recommendations_product2);

    List<String> productList = new ArrayList<String>(); 
    list.add("traveling"); 
    list.add("short"); 
    list.add("not much");

    List<String> answerList = new ArrayList<String>();
    mRef = FirebaseDatabase.getInstance().getReference().child("BTSpeakers");
    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if(dataSnapshot.exists())
            {
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                String answer = ds.child("answer").getValue().toString();
                answerList.add(answer);
                if(productList.equals(answerList)){
                    productTwo.setText("Speakers");
                }

              }
           }
       }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    }

First, add the elements inside the list, then add a reference to your parent node BtSpeakers , then loop inside your datasnapshot and retrieve all the answers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM