我是Firebase和Android的新手。我想创建一个应用程序,用户可以在其中输入他/她想要拥有的多种食物并将其保存到Firebase。现在,我的问题是,与其使用唯一的密钥一一保存数据,不如保存相同的数据实例。它并没有遍历其余输入。
这是我的代码,用于用户生成的editText字段并将其保存到Firebase。
public class AddMenuActivity extends AppCompatActivity {
private EditText menu_added;
private LinearLayout lnrDynamicEditTextHolder;
private EditText edtNoCreate;
private Button btnCreate, btnSave;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_menu);
lnrDynamicEditTextHolder = (LinearLayout) findViewById(R.id.lnrDynamicEditTextHolder);
edtNoCreate = (EditText) findViewById(R.id.edtNoCreate);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnSave = (Button) findViewById(R.id.btnSave);
auth = FirebaseAuth.getInstance();
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(edtNoCreate.getText().toString().length()>0) {
try {
lnrDynamicEditTextHolder.removeAllViews();
} catch (Throwable e) {
e.printStackTrace();
}
final int length = Integer.parseInt(edtNoCreate.getText().toString());
for (int i=0;i<length;i++){
final EditText editText = new EditText(AddMenuActivity.this);
editText.setId(i+1);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("EditText "+(i+1));
lnrDynamicEditTextHolder.addView(editText);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int a=0;a<length;a++){
final String menu_add = editText.getText().toString().trim();
Query menuQuery = FirebaseDatabase.getInstance().getReference().child("Establishment").orderByChild("Estabname").equalTo(menu_add);
menuQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount()>0){
Toast.makeText(AddMenuActivity.this,"This Name has been taken", Toast.LENGTH_SHORT).show();
}
else{
if (TextUtils.isEmpty(menu_add)) {
Toast.makeText(getApplicationContext(), "Enter an Item!", Toast.LENGTH_SHORT).show();
return;
}
String menu_id = auth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Menu");
DatabaseReference pushedPostRef = current_user_db.push();
String postId = pushedPostRef.getKey();
DatabaseReference current_user_db2 = FirebaseDatabase.getInstance().getReference().child("Menu").child(postId);
Map newPost = new HashMap();
newPost.put("Mfood_Name", menu_add);
current_user_db2.setValue(newPost);
startActivity(new Intent(AddMenuActivity.this, ManageEstablishmentActivity.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
});
}
}
}
});
}
这是XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtNoCreate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Number Of Food's"
android:inputType="number" />
<Button
android:id="@+id/btnCreate"
android:layout_width="76dp"
android:layout_height="match_parent"
android:background="@drawable/rounded_button"
android:layout_margin="6dp"
android:text="Create" />
<Button
android:id="@+id/btnSave"
android:layout_width="76dp"
android:layout_height="match_parent"
android:background="@drawable/rounded_button"
android:layout_margin="6dp"
android:text="Save" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<LinearLayout
android:id="@+id/lnrDynamicEditTextHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 0 :(得分:0)
这样做-
Map newPost = new HashMap();
newPost.put("Mfood_Name", menu_add);
newPost.put("mfood_Quantity", itemQuantity);
current_user_db2.setValue(newPost);
答案 1 :(得分:0)
在Firebase Realtime数据库/ Firestore中,您可以以Hashmap(Realtime / Firestore)的形式存储数组和列表。
Cloud Firestore的示例代码片段:
public class MapPost {
String title;
Map<String,Boolean> categories;
public MapPost(String title, Map<String,Boolean> categories) {
this.title = title;
this.categories = categories;
}
}
Map<String, Boolean> categories = new HashMap<>();
categories.put("technology", true);
categories.put("opinion", true);
categories.put("cats", true);
MapPost myMapPost = new MapPost("My great post", categories);