我想將數據添加到我擁有的Firebase列表中,並且我感覺我很接近。如何添加到Firebase列表中?
public class AddRestaurantActivity extends AppCompatActivity {
private EditText newRestaurant;
private Button addBtn;
private Button homeBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_restaurant);
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
final DatabaseReference mRestReference = mRootRef.child("restaurants");
newRestaurant = (EditText) findViewById(R.id.addrestauranttext);
addBtn = (Button) findViewById(R.id.addrestaurantBtn);
homeBtn = (Button) findViewById(R.id.HomeButton);
addBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
String newRestaurantName = newRestaurant.getText().toString();
mRestReference.getRoot().setValue(newRestaurantName);
newRestaurant.setText("");
}
});
是我到目前爲止,當我用這個它不工作的代碼。問題是設置新項目。現在它完全取代了我的數據庫,因爲mRestReference.getRoot()。setValue(newRestaurantName);代碼。我需要找到添加新餐廳的方法,以便它不會替換數據庫中已有的所有內容。參考數據庫的圖片來看看它是如何設置的。只有一個名爲name的屬性非常簡單。 我是新來的android和一般編碼,所以任何幫助將是偉大的。
請更具體的比「它不工作」。是否有例外或其他錯誤信息? – DavidS
我試圖添加更多的細節。這些幫助有用? – Hunter
請參閱https://firebase.google.com/docs/database/android/lists-of-data#append_to_a_list_of_data –