简体   繁体   English

如何在一个键中保存两个 EditText 值

[英]How to save two EditText values ​in a key

This is my code这是我的代码

final String name = subjectEditText.getText().toString();
final String desc = descEditText.getText().toString();

String DateNow = new SimpleDateFormat("ddmmyyyy", Locale.getDefault()).format(new Date());
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("products/" + DateNow);
myRef.setValue(name, desc);

But the two values are not being saved, only the name.但是这两个值没有被保存,只有名称。

The second value you pass to setValue is being set as the priority of the node. 您传递给setValue的第二个值被设置为节点的优先级。 This priority isn't visible in the Firebase console, but it stored and retrieved through the API.此优先级在 Firebase 控制台中不可见,但它通过 API 存储和检索。 If you get a DataSnapshot for the node, you can use getPriority() to get the desc back.如果您获得节点的DataSnapshot ,则可以使用getPriority()来获取desc

Priorities are mostly a leftover from the olden days of this API, and serve very little useful purpose these days.优先级主要是从这个 API 的旧时代遗留下来的,这些天几乎没有什么用处。

Nowadays if you want to store multiple values under a node, you should give them names.现在如果你想在一个节点下存储多个值,你应该给它们命名。 For example:例如:

final String name = subjectEditText.getText().toString();
final String desc = descEditText.getText().toString();

String DateNow = new SimpleDateFormat("ddmmyyyy", Locale.getDefault()).format(new Date());
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("products/" + DateNow);
Map<String, Object> values = new HashMap<>();
values.put("name", name);
values.put("desc", desc);
myRef.setValue(values);

This will create a little JSON structure under DateNow with:这将在 DateNow 下创建一个小的DateNow结构:

{
  "name": "the name from the text field",
  "desc": "the value from the desc field"
}

You can then read these value back with:然后,您可以使用以下命令读取这些值:

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i("database", dataSnapshot.child("name").getValue(String.class);
        Log.i("database", dataSnapshot.child("desc").getValue(String.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

Using map.使用 map。

String DateNow = new SimpleDateFormat("ddmmyyyy", 
Locale.getDefault()).format(new Date());
FirebaseDatabase database = 
FirebaseDatabase.getInstance();
DatabaseReference myRef = 
database.getReference("products/" + DateNow);
final String name = 
subjectEditText.getText().toString();

final String desc = 
descEditText.getText().toString();
Map<String, Object> data = new HashMap<> ();
data.put("name", name);
data.put("desc", desc);
myRef.set (data).addOnSuccussListener(...)...;

You can instead replace您可以改为替换

data.put("name", name);
data.put("desc", desc);

with

data.put("name_desc_array", name_desc_array);

But you have to create array with但是你必须创建数组

ArrayList<String> name_desc_array = new ArrayList();
name_desc_array.add ("name");
name_desc_array.add ("desc");

Remember, using map is the best way请记住,使用 map 是最好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM