我已经使用 Tools -> Firebase 为应用程序设置了 Firebase,并将 Internet 访问添加到 manifest。我试图从孩子那里获取字符串以保存在应用程序上,以便以后在进一步开发时使用它们。我不关心身份验证,所以据我收集的用户 ID 过程是无关紧要的。主文件的第一部分有写作方面,当其他所有内容都被注释掉时,应用程序运行并正常工作。所以问题出在阅读上。
// MainActivity.java:
public class MainActivity extends AppCompatActivity {
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference("Strings");
Button sendBtn = (Button) findViewById(R.id.sendBtn);
EditText stringText = (EditText) findViewById(R.id.stringText);
TextView textView = (TextView) findViewById(R.id.textView);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("I can send text to this from Android App");
}
});
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot) {
StringExchange stringX = new StringExchange();
stringX.setPhoneNum(dataSnapshot.getValue(StringExchange.class).getPhoneNum());
stringX.setTrapID(dataSnapshot.getValue(StringExchange.class).getTrapID());
stringX.setActive(dataSnapshot.getValue(StringExchange.class).getActive());
Toast.makeText(getBaseContext(), "Strings Saved", Toast.LENGTH_SHORT).show();
}
}
// class: StringExchange.java:
package com.snaresense.firebase;
/**
* Created by Kevin on 10/21/2017.
*/
public class StringExchange {
String PhoneNum;
String Active;
String TrapID;
public StringExchange(){
}
public String getPhoneNum(){
return PhoneNum;
}
public void setPhoneNum(String PhoneNum){
this.PhoneNum = PhoneNum;
}
public String getActive(){
return Active;
}
public void setActive(String Active){
this.Active = Active;
}
public String getTrapID(){
return TrapID;
}
public void setTrapID(String TrapID){
this.TrapID = TrapID;
}
}