英文:
How to save two EditText values in a key
问题
以下是翻译好的内容:
这是我的代码
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);
但是两个值没有被保存,只有名字。
英文:
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.
答案1
得分: 1
第二个传递给 setValue
的值被设置为节点的优先级。这个优先级在 Firebase 控制台中不可见,但通过 API 存储和检索。如果您获取了节点的 DataSnapshot
,您可以使用 getPriority()
来获取 desc
。
优先级在这个 API 的旧时代大多是遗留下来的,现在很少有实际用途。
现在,如果您想在一个节点下存储多个值,应该为它们赋予名称。例如:
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);
这将在 DateNow
下创建一个小的 JSON 结构,如下所示:
{
"name": "文本字段中的名称",
"desc": "desc 字段中的值"
}
然后,您可以使用以下方式读取这些值:
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();
}
});
英文:
The second value you pass to setValue
is being set as the priority of the node. This priority isn't visible in the Firebase console, but it stored and retrieved through the API. If you get a DataSnapshot
for the node, you can use getPriority()
to get the desc
back.
Priorities are mostly a leftover from the olden days of this API, and serve very little useful purpose these days.
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:
{
"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();
}
}
答案2
得分: 0
Using 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).addOnSuccessListener(...);
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
英文:
Using 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论