简体   繁体   English

Android Firebase 查询返回 Null

[英]Android Firebase query return Null

I'm working on a project where there's an activity which contains 3 spinners( Nested).我正在做一个项目,其中有一个包含 3 个微调器(嵌套)的活动。 I need the 2nd spinner to have the data based on the selected item of the 1st spinner and the data is retrieved from Firebase我需要第二个微调器根据第一个微调器的选定项目获取数据,并且从 Firebase 检索数据

在此处输入图像描述

The 1st spinner is going to have all "Gouv" values, the 2nd one is going to have the "Deleg" values where "Gouv" value is equal to the selected item "Gouv" of the 1st spinner.第一个微调器将具有所有“Gouv”值,第二个将具有“Deleg”值,其中“Gouv”值等于第一个微调器的选定项“Gouv”。

Here is the code that i tried and i keep get in the log Null.这是我尝试过的代码,我一直在日志 Null 中。

public class RechCode extends AppCompatActivity {
    DatabaseReference spinnerRef;
    Query spinnerRefG;
    Spinner spinnerG;
    Spinner spinnerD;
    Spinner spinnerL;
    ArrayAdapter<String> adapterG;
    ArrayAdapter<String> adapterL;
    ArrayAdapter<String> adapterD;
    ArrayList<String> spinnerDList;
    ArrayList<String> spinnerGList;
    ArrayList<String> spinnerLOList;
    Query spinnerRefD;
    private String ChoixG;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rech_code);
        spinnerG = findViewById(R.id.SpinnerGouv);
        spinnerD = findViewById(R.id.SpinnerDeleg);
        spinnerL = findViewById(R.id.SpinnerLoc);
        spinnerRef = FirebaseDatabase.getInstance().getReference().child("CodePostale");
        

        //Query queryD = spinnerG.("state", "CA");
        spinnerGList = new ArrayList<>();
        spinnerDList = new ArrayList<>();
        spinnerLOList = new ArrayList<>();
        adapterD= new ArrayAdapter<String>( RechCode.this, android.R. layout. simple_spinner_dropdown_item,spinnerDList);
        adapterL= new ArrayAdapter<String>( RechCode.this, android.R. layout. simple_spinner_dropdown_item,spinnerLOList);
        adapterG= new ArrayAdapter<String>( RechCode.this, android.R. layout. simple_spinner_dropdown_item,spinnerGList);
        spinnerG.setAdapter(adapterG);
        spinnerD.setAdapter(adapterD);
        spinnerL.setAdapter(adapterL);
        ShowdataGouv();
        spinnerRefG= FirebaseDatabase.getInstance().getReference("CodePostale").child("Deleg").orderByChild("Gouv").equalTo(ChoixG);

        ShowdataDeleg();
        spinnerG.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                ChoixG = spinnerGList.get(i);
                Toast.makeText(getApplicationContext(), "Gouvernorat:" + ChoixG, Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });



        Button retour = (Button) findViewById(R.id.return_btn);

        retour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RetourMenu();
            }
        });




    }


    private void ShowdataLoc(String text) {
        spinnerRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot1) {
                for (DataSnapshot item:snapshot1.getChildren()){
                    spinnerLOList.add(item.child("Loc").getValue().toString());

                }
                adapterD.notifyDataSetChanged();

            }

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

            }
        });
    }

    private void ShowdataDeleg() {
        spinnerRefG.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot2) {
                for (DataSnapshot item:snapshot2.getChildren()){
                    spinnerDList.add(item.child("Deleg").getValue().toString());


                }
                Log.d("Value","BBBBBBBB"+ snapshot2);
                adapterD.notifyDataSetChanged();


            }

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

            }
        });
    }

    private void ShowdataGouv() {
        spinnerRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot3) {
                for (DataSnapshot item:snapshot3.getChildren()){
                    spinnerGList.add(item.child("Gouv").getValue().toString());
                    Log.d("Value","AAAAAAAAA"+ snapshot3);

                }
                adapterG.notifyDataSetChanged();

            }

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

            }
        });
    }


    private void RetourMenu() {
        Intent intent = new Intent(getApplicationContext(), Menus.class);
        startActivity(intent);
    }
    

}

在此处输入图像描述

The problem is in the way you construct the query here:问题在于您在此处构建查询的方式:

spinnerRefG= FirebaseDatabase.getInstance()
    .getReference("CodePostale")
    .child("Deleg")
    .orderByChild("Gouv")
    .equalTo(ChoixG);

You're asking to load child nodes of /CodePostale/Deleg that have a Gouv property with a value of ChoixG .您要求加载/CodePostale/Deleg的子节点,其Gouv属性的值为ChoixG But if we check the screenshot of the data, there is no /CodePostale/Deleg , so no data is returned.但是如果我们查看数据的截图,没有/CodePostale/Deleg ,所以没有返回数据。

What you want instead is to check the child nodes of /CodePostale and return the ones where their Deleg/Gouv property has a value of ChoixG , which you can do with:相反,您需要检查/CodePostale的子节点并返回其Deleg/Gouv属性值为ChoixG的子节点,您可以使用以下方法:

spinnerRefG= FirebaseDatabase.getInstance()
    .getReference("CodePostale")
    .orderByChild("Deleg/Gouv")
    .equalTo(ChoixG);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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