gpt4 book ai didi

java - 我的 firebase 实时数据库路径不起作用

转载 作者:行者123 更新时间:2023-12-04 08:30:22 24 4
gpt4 key购买 nike

我正在制作一个基于 firebase 实时数据库的应用程序。
我希望我的数据库结构像下面给出的树一样:-
enter image description here
我正在尝试通过以下 java 代码将这些数据写入我的 firebase 实时数据库中:-

public class AddActivity extends AppCompatActivity {

private EditText etName;
EditText etRoll;
Button btnAdmit;
Button btnView;

String stName;
String stRoll;
FirebaseDatabase myfire;
DatabaseReference myRef;

@Override
public void onBackPressed() {

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etName = (EditText) findViewById(R.id.idUser);
etRoll = (EditText) findViewById(R.id.idPass);
btnView = (Button) findViewById(R.id.idView);

btnAdmit= (Button) findViewById(R.id.idAdmit);
myfire = FirebaseDatabase.getInstance();

myRef = myfire.getReference("Users")
.child("uid");// I dont know what
// to write in this child
// in stead of
// "uid"


btnAdmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

stName = etName.getText().toString();
stRoll = etRoll.getText().toString();


etName.setText("");
etRoll.setText("");



myRef.child("101").addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

model model =new basic();
model.setFb01name(stName);
model.setFb04roll(stRoll);
myRef.setValue(model);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_id));
TextView text = (TextView) layout.findViewById(R.id.idToast);
text.setText("Added.");
Toast toast = new Toast(AddActivity.this);
toast.setGravity(Gravity.CENTER ,0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
// btnAdmit.setEnabled(false);
// btnAdmit.setVisibility(View.GONE);
}




@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_id));
TextView text = (TextView) layout.findViewById(R.id.idToast);
text.setText(" Database Error.");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER ,0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

DatabaseError e = null;

Log.e("Database", "Error", e.toException());
}



});
}
});

我的模型类是这样的:
public class model {

String fb01name;
String fb04roll;

public model() {

}

public model(String fb01name, String fb04roll) {
this.fb01name = fb01name;
this.fb04roll = fb04roll;
}



public String getFb01name() {
return fb01name;
}

public void setFb01name(String fb01name) {
this.fb01name = fb01name;
}

public String getFb04roll() {
return fb04roll;
}

public void setFb04roll(String fb04roll) {
this.fb04roll = fb04roll;
}
}

logcat 错误消息中没有任何内容。我没有在 firebase 实时数据库中写入任何内容。
我无法找到错误。该应用程序正在显示数据更改方法中给出的“添加”的 toast 。我的数据库规则是这样的:
{
// Allow anyone to read data, but only authenticated content owners can
// make changes to their data

"rules": {
"Users": {
"$uid": {
".read": true,
// or ".read": "auth.uid != null" for only authenticated users
".write": "auth.uid == $uid"
}
}
}
}

请调查此事并提供您的见解和实际解决方案。

最佳答案

试试下面的代码

public class AddActivity extends AppCompatActivity {

private EditText etName;
EditText etRoll;
Button btnAdmit;
Button btnView;

String stName;
String stRoll;
FirebaseDatabase myfire;
DatabaseReference myRef;

@Override
public void onBackPressed() {

}

private String getUID() {
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
if (mUser != null) {
String strUID = mUser.getUid();
if (!TextUtils.isEmpty(strUID)) {
return strUID;
}
}

return "";

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etName = (EditText) findViewById(R.id.idUser);
etRoll = (EditText) findViewById(R.id.idPass);
btnView = (Button) findViewById(R.id.idView);

btnAdmit = (Button) findViewById(R.id.idAdmit);
myfire = FirebaseDatabase.getInstance();

String strUID = getUID();
if (TextUtils.isEmpty(strUID)) {
//handle case of null UID
}

myRef = myfire.getReference("Users/" + strUID);


btnAdmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

stName = etName.getText().toString();
stRoll = etRoll.getText().toString();


etName.setText("");
etRoll.setText("");



myRef.child("101").addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

model model = new basic();
model.setFb01name(stName);
model.setFb04roll(stRoll);
myRef.setValue(model);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_id));
TextView text = (TextView) layout.findViewById(R.id.idToast);
text.setText("Added.");
Toast toast = new Toast(AddActivity.this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
// btnAdmit.setEnabled(false);
// btnAdmit.setVisibility(View.GONE);
}




@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_id));
TextView text = (TextView) layout.findViewById(R.id.idToast);
text.setText(" Database Error.");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

DatabaseError e = null;

Log.e("Database", "Error", e.toException());
}



});
}
});

关于java - 我的 firebase 实时数据库路径不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65049872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com