向成分

时间:2017-10-05 15:51:22

标签: java android json database firebase

我想让我的数据库看起来像这样 FirebaseDB

我通过循环它们从arrayList添加成分'ingredientName。
我的问题是如何轻松保存数量。当用户添加ingredientName时,测量将来自另一个DB路径 (例如“milk”)到ListView。(但这只是一个未来的计划,首先我要解决数量)
我是否必须将EditText添加到list_item.xml?或者我怎样才能轻松获得数量然后保存?
IngredName保存方法完美无缺,但我不知道如何轻松存储每个ingredientName的数量。

来自AddIngredients活动的

private String key;
private ArrayList<String> arrayList=new ArrayList<>();
ListView listView=(ListView) findViewById(R.id.listv);
txtInput=(EditText) findViewById(R.id.IngredientsInput);
private DatabaseReference ingDBref = FirebaseDatabase.getInstance().getReference("Ingredients");
private DatabaseReference DBref= FirebaseDatabase.getInstance().getReference("Recipes");

adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.ingredientName,arrayList);
listView.setAdapter(adapter);

public void addIngredients(){
        final String IngredientName=txtInput.getText().toString();

        ingDBref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                    if(data.child("ingredientName").getValue().equals(IngredientName)){                        
            //If ingredientName=allowed ingredientNames from DB
                        arrayList.add(IngredientName);
                        adapter.notifyDataSetChanged();
            Toast.makeText(this,"Added to ListView...",Toast.LENGTH_LONG).show();
                    }
                    else{
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }


public void saveIngredients(){
    key=DBref.child.push().getKey();
        String ingredientName;
        for(int i=0;i<arrayList.size();i++){
            ingredientName=arrayList.get(i);
            Ingredients ingredients=new Ingredients(ingredientName);
            DBref.child(key).child("Ingredients").child(ingredientName).setValue(ingredients);
        }

成分类:

public class Ingredients {

    String ingredientName;
   // String ingredientQty;
   // String measureName;

    public Ingredients(){
    }

    public Ingredients(String IngredientName){
        this.ingredientName=IngredientName;
       // this.ingredientQty=IngredientQty;
        //this.measureName=MeasureName;
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public String getIngredientName(){
        return ingredientName;
    }
/*
    public String getIngredientQty(){
        return ingredientQty;
    }*/
    /*
    public String getMeasureName(){
        return measureName;
    }*/
}

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/ingredientName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

JsonConvert.SerializeObject(new { item = dt });

the impoartant属于成分。