遍历具有用户ID的firebase节点,以显示特定的子代并将值存储在数组中

时间:2019-04-16 21:09:02

标签: android firebase firebase-realtime-database

我试图遍历firebase中的一个节点以为所有用户检索该节点中的特定字段,而不管登录的userID如何。

  -Geo_code
        -9rsgncjadHFssH
           ChemistName:"Rayan"
           Geo_codes: "25.42,0.1426"
        -yUIoobF5gfhvGFG
           ChemistName: "Mugash"
           Geo_codes: "28.42,0.1426" 
        -JgHF6Yj7feNNGKGBH
           ChemistName: "Stephany" 
           Geo_codes: "30.42,1.1426"

    locate1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Query query = FirebaseDatabase.getInstance().getReference().child("Geocode");
                query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    ArrayList<String> code = new ArrayList<>();
                    ArrayList<Float> distances = new ArrayList<Float>();
                    String str[];

                    float distanceInMeters = 0;
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                        String coordinates = snapshot.child("Geo_codes").getValue().toString();
                        code = new ArrayList<>();
                        code.add(coordinates);
                        Log.d(TAG, "All coordinates are" + code);
                    }

我希望代码ArrayList中具有所有Geo_codes节点值

2 个答案:

答案 0 :(得分:0)

Create a Geo_Code pojo class then add a List<Geo_Code>geo_codeList() = new ArrayList();
in your activity. onDataChange Geo_Code geo_code  = datasnapshot.getValue(Geo_Code.class); geo_codeList.add(geo_code);
 for(int i=0; i<geo_codeList.size(); i++){
String cordinates = geo_codeList.get(i);
         }

答案 1 :(得分:0)

所以我不知道您正在为geo_codes使用什么,我只是使用了一个字符串,但这是pojo

    public class Geo_Code {
    public String ChemistName;
    public String Geo_Codes;

    public Geo_Code(){
    }

    public Geo_Code(String chemistName, String geo_Codes) {
        ChemistName = chemistName;
        Geo_Codes = geo_Codes;
    }
}

这是获取所有ChemistName和Geo_Code的代码。如果您仍有问题,请随时提出任何问题。

   public class MainActivity extends AppCompatActivity {
public List<Geo_Code> geo_codes = new ArrayList<>();

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


        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Setting the data with our Pojo class
                //Geo_Code s  = new Geo_Code("Rayan","25.42,0.1426");
                //FirebaseDatabase.getInstance().getReference("Geo_Code").setValue(s);
                FirebaseDatabase.getInstance().getReference("Geo_Code").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        //Get our snapshot and add it to a list of Geo_Code
                        Geo_Code geo_code = dataSnapshot.getValue(Geo_Code.class);
                        geo_codes.add(geo_code);

                        //Looping through geo_codes and getting all the data.
                        for(int i=0; i<geo_codes.size(); i++) {
                            String name = geo_codes.get(i).ChemistName;
                            String cordinates = geo_codes.get(i).Geo_Codes;
                        }
                    }

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

                    }
                });
            }
        });
    }


}