当前位置: 代码迷 >> java >> onDataChange方法在android studio中不起作用
  详细解决方案

onDataChange方法在android studio中不起作用

热度:7   发布时间:2023-07-31 11:54:58.0

我正在使用Android Studio应用程序,并且使用了Firebase实时数据库。 而且我有一个问题,请寻求帮助。数据库中的表名称为Hospital ,属性为hospital_namehospital_codehospital_passwordhospital_phonehospital_email

信息添加成功,但是我无法从数据库中检索信息(医院名称和医院代码),因此我搜索并使用了不同的方法,但是仍然存在问题。 应该的代码从Hospital表中检索信息,并将其显示在应用程序中。

public class MainActivity extends AppCompatActivity  {
    TextView reg;

    EditText email,password;
    Button loginBt;
    Database db;
    String emailString,passwordString;
    TextView regText;
    TextView name,code;
    private DatabaseReference ref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ref = FirebaseDatabase.getInstance().getReference().child("Hospital").child("ABA");
        db = new Database();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        email = (EditText) findViewById(R.id.logInEmail);
        password = (EditText) findViewById(R.id.passwordLogin);
        regText = (TextView) findViewById(R.id.reg_text);
        loginBt = (Button) findViewById(R.id.logInButton);
       /* loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });*/
        regText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createAccount();
            }
        });
        name = (TextView) findViewById(R.id.hospitalNameMain);
        code = (TextView) findViewById(R.id.hospitalCodeMain);
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });


    }

    private void show() {

        ref = FirebaseDatabase.getInstance().getReference().child("Hospital").child("QCH");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Toast.makeText(MainActivity.this,"log in ",Toast.LENGTH_SHORT).show();
                String hospitalName = dataSnapshot.child("hospital_name").getValue().toString();
                String hospitalCode = dataSnapshot.child("hospital_code").getValue().toString();
                name.setText(hospitalName);
                code.setText(hospitalCode);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private void createAccount() {
        Intent i = new Intent(this, RegistrationActivity.class);
        startActivity(i);
    }
}

数据库结构:

不要使用.child("Hospital").child("QCH")作为参考

您的ref将在此

FirebaseDatabase.getInstance().getReference().child("Hospital")

您将获得dataSnapshot所有记录

编辑

尝试将dataSnapshot转换为您的Hospital模型并从中获取值

FirebaseDatabase.getInstance()。getReference()子( “医院”)。子女( “QCH”)

如果您想通过身份证获得特定的医院。

使用以下查询。

databaseRef.orderByChild("hostpital_code").equals(yourCode);

因此,您的完整查询将是:

ref = FirebaseDatabase.getInstance().getReference().child("Hospital").orderByChild("hostpital_code").equals("QCH");

如果您有任何问题,请告诉我。

根据您的评论:

用于医院代码=“ QCH”的单个记录

请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Hospital").child("QCH");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String hospitalName = dataSnapshot.child("hospital_name").getValue(String.class);
        String hospitalCode = dataSnapshot.child("hospital_code").getValue(String.class);
        Log.d(TAG, hospitalName + " / " + hospitalCode);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
ref.addListenerForSingleValueEvent(valueEventListener);

您的logcat中的结果将是:

hjkk / QCH
  ref = FirebaseDatabase.getInstance().getReference().child("Hospital");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //use for loop iterator here 
    for(DataSnapshot data1:dataSnapshot){
     //this will print the key and value in console use it as you wish.
        log.i("TAG",data1.child().getKey()+"::"+data1.child.getValue()); 
    }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
  相关解决方案