如何将此数据添加到Firebase?

时间:2019-08-06 17:51:05

标签: android firebase firebase-realtime-database nosql

enter image description here

这是我构建数据库的方式,我想从burger(由用户提供)中添加节点,我已经使用过:

mDatabase.child(rid).child(Category).child("name"+i).setValue(itemClass); 要实现这一目标,

类别〜汉堡

名称〜名称0

itemClass是具有两个字段的类:名称和价格。

但是按预期,这是行不通的。为什么以及如何实现这一目标?

我有一个活动,用户可以从中添加食物的类别以及与该类别相关的食物(例如汉堡)。我基本上想将类别节点附加到餐厅ID(由rid提供)

public void updateToDatabase() {
    ItemClass itemClass = new ItemClass();
    String Price;
    String Category = category_name.getText().toString();
    String rid,name,price;
    EditText et_Name;
    EditText et_Price;

    //Initialize
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("Restaurant_Pref",0);
    rid = preferences.getString("rid",null);
    mDatabase = FirebaseDatabase.getInstance().getReference("Menu");


    for (int i=0; i <= linearLayout.getChildCount()-1;i++) {
        Log.d("DB", "updateToDatabase: "+ linearLayout.getChildAt(i).toString());
        View view_CardLayout = linearLayout.getChildAt(i);
        if (view_CardLayout instanceof CardView) {
            Log.d("DB", "updateToDatabase: " + ((CardView) view_CardLayout).getChildCount());
            Log.d("DB", "updateToDatabase: " + ((CardView) view_CardLayout).getChildAt(0));

            View view_LinearLayout = ((CardView) view_CardLayout).getChildAt(0);
            if (view_LinearLayout instanceof LinearLayout) {
                Log.d("DB", "updateToDatabase: " + ((LinearLayout) view_LinearLayout).getChildCount());
                Log.d("DB", "updateToDatabase: " + ((LinearLayout) view_LinearLayout).getChildAt(0));

                View et_name = ((LinearLayout) view_LinearLayout).getChildAt(0);
                View et_price = ((LinearLayout) view_LinearLayout).getChildAt(1);

                if (et_name instanceof EditText) {

                    et_Name = ((EditText) et_name);
                    name = et_Name.getText().toString();
                    itemClass.setName(name);
                    Log.d("Logs:", "updateToDatabase: " + et_Name.getText().toString());
                }
                if (et_price instanceof EditText) {
                   et_Price = ((EditText) et_price);
                    price = et_Price.getText().toString();
                    itemClass.setPrice(Integer.parseInt(price));
                    Log.d("Logs:", "updateToDatabase: " + et_Price.getText().toString());
                }
            }
        }
        mDatabase.child(rid).child(Category).child("name"+i).child("name").setValue(itemClass.getName());
        mDatabase.child(rid).child(Category).child("name"+i).child("price").setValue(itemClass.getPrice());
    }
}

0 个答案:

没有答案