我正在编写一个应用程序,允许用户通过将数据上传到firebase托管的数据库来协作处理库存。我可以写数据给它没问题,但它只是想让我在触发事件时检索数据,例如数据被更改。我想要的只是检索一个我保存到数据库中的字符串,并将其设置为我页面上TextView元素的文本值,我想在单击按钮时发生这种情况。我该怎么做?
这是用户写入数据的页面的代码:
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
String dbLocationTag = location.getText().toString();
String itemInfo = "Item Description: " + itemDescription.getText().toString() + "\n" + "Job Number: " + jobNumber.getText().toString();
mDatabase.setValue(dbLocationTag, itemInfo);
Context context = getApplicationContext();
final Intent inventoryHomeScreen = new Intent();
inventoryHomeScreen.setClass(context, Inventory_Management.class);
startActivity(inventoryHomeScreen);
}
});
我希望能够检索数据:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory__view__item);
final TextView itemInfo = (TextView) findViewById(R.id.itemInfoTextView);
final Button viewButton = (Button) findViewById(R.id.viewButton);
assert viewButton != null;
viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//I would like to set the text of itemInfo to the data I just wrote
}
});
}
答案 0 :(得分:1)
您需要使用addListenerForSingleValueEvent()
来检索数据
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("<childName>").addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
在child
中,您需要提及您的子节点名称
如果您想要更改整个数据库,只需删除child
,使其看起来像mDatabase.addListenerForSingleValueEvent()
答案 1 :(得分:0)
您可以使用addValueEventListener()
在触发事件时检索数据。
在onClick()
方法中,您可以写:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
// In the database whenever any data is changed then the below code snipped is going to be executed.
mDatabase.child("<dbLocationTag>").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// the changed value is going to be stored into the dataSnapshot
// to retrieve value from dataSnapshot we write
String value = dataSnapshot.getValue(String.class);
mValueView.setText(value);
// NOTE: if you've multiple childs of same node then you can use Map to display all of them : EG:
/*Map<String, String> map = dataSnapshot.getValue(Map.class);
String val1 = map.get("val1");
String val2 = map.get("val2");
String val3 = map.get("val3");
Log.v("TAG", "Value 1 "+val1);
Log.v("TAG", "Value 2 "+val2);
Log.v("TAG", "Value 3 "+val3);
*/
}
@Override
public void onCancelled(FirebaseError firebaseError) {
mValueView.setText(firebaseError.toString());
}
});
答案 2 :(得分:0)
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference mRef = mDatabaseReference.getDatabase().getReference("users");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Log.d(TAG, "" + ds.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
}
});
mRef.addListenerForSingleValueEvent(valueEventListener);
以上方法对我有用。希望它对您也有帮助。