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

huangapple go评论70阅读模式
英文:

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(&quot;ddmmyyyy&quot;, Locale.getDefault()).format(new Date());
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(&quot;products/&quot; + DateNow);
Map&lt;String, Object&gt; values = new HashMap&lt;&gt;();
values.put(&quot;name&quot;, name);
values.put(&quot;desc&quot;, desc);
myRef.setValue(values);

This will create a little JSON structure under DateNow with:

{
  &quot;name&quot;: &quot;the name from the text field&quot;,
  &quot;desc&quot;: &quot;the value from the desc field&quot;
}

You can then read these value back with:

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i(&quot;database&quot;, dataSnapshot.child(&quot;name&quot;).getValue(String.class);
        Log.i(&quot;database&quot;, dataSnapshot.child(&quot;desc&quot;).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(&quot;ddmmyyyy&quot;, 
Locale.getDefault()).format(new Date());
FirebaseDatabase database = 
FirebaseDatabase.getInstance();
DatabaseReference myRef = 
database.getReference(&quot;products/&quot; + DateNow);
final String name = 
subjectEditText.getText().toString();

final String desc = 
descEditText.getText().toString();
Map&lt;String, Object&gt; data = new HashMap&lt;&gt; ();
data.put(&quot;name&quot;, name);
data.put(&quot;desc&quot;, desc);
myRef.set (data).addOnSuccussListener(...)...;

You can instead replace

data.put(&quot;name&quot;, name);
data.put(&quot;desc&quot;, desc);

with

data.put(&quot;name_desc_array&quot;, name_desc_array);

But you have to create array with

ArrayList&lt;String&gt; name_desc_array = new ArrayList();
name_desc_array.add (&quot;name&quot;);
name_desc_array.add (&quot;desc&quot;);

Remember, using map is the best way

huangapple
  • 本文由 发表于 2020年8月6日 23:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63286894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定