我正在做一个基于firebase实时数据库的应用程序。
我希望我的数据库结构像给定的树一样below:-
我正试图通过以下java语言将这些数据写入firebase实时数据库code:-
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“added”,如on data change method中所示。我的数据库规则如下:
{
// 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"
}
}
}
}
请调查此事,并提供您的见解和切实可行的解决办法。
1条答案
按热度按时间vjhs03f71#
尝试以下代码