我正在创建一个应用,并尝试使用Firebase实时数据库存储我的游戏得分。我的数据库有很多用户,每个用户都包含一个以日期为键,分数为值的TreeMap。我试图让玩家单击一个按钮,并将其得分保存在TreeMap中。我在读取TreeMap中的数据时遇到了麻烦,因为我希望这是一次读取而不是侦听器。
我尝试过首先检索地图,对其进行更新,然后将其重新上传到数据库。但是,由于在线教程似乎都使用侦听器和快照,因此我不知道如何将其链接到按钮单击。
private void showData(DataSnapshot dataSnapshot){
String name = "";
String age = "";
String number = "";
TreeMap<String, Integer> scores = new TreeMap<>();
for (DataSnapshot ds: dataSnapshot.getChildren()){
User user = new User();
if (ds.child(userID).getValue(User.class).getScores() != null){
ds.child(userID).getValue(User.class).getScores().put(date, totalScore);
scores = ds.child(userID).getValue(User.class).getScores();
}
name = ds.child(userID).getValue(User.class).getUserName();
age = ds.child(userID).getValue(User.class).getUserAge();
number = ds.child(userID).getValue(User.class).getUserPhoneNumber();
}
scores.put(date, totalScore);
User temp = new User(userID, name, age, number, scores);
myRef.child(userID).setValue(temp);
}
这是我编写的解决方案,但没有包含单击按钮的功能。
答案 0 :(得分:1)
prog.cc: In function 'int main()':
prog.cc:23:49: warning: 'void* memcpy(void*, const void*, size_t)' copying an object of type 'boost::endian::big_uint16_buf_t' {aka 'class boost::endian::endian_buffer<boost::endian::order::big, short unsigned int, 16>'} with 'protected' member 'boost::endian::endian_buffer<boost::endian::order::big, short unsigned int, 16>::m_value' from an array of 'std::array<std::byte, 6>::value_type' {aka 'enum class std::byte'}; use assignment or copy-initialization instead [-Wclass-memaccess]
23 | std::memcpy(&v, buffer.data(), sizeof(v));
| ^
In file included from prog.cc:2:
/opt/wandbox/boost-1.70.0/gcc-9.1.0/include/boost/endian/buffers.hpp:375:11: note: 'boost::endian::big_uint16_buf_t' {aka 'class boost::endian::endian_buffer<boost::endian::order::big, short unsigned int, 16>'} declared here
375 | class endian_buffer< order::big, T, n_bits, align::no >
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这是您更新分数的方法,您没有显示您的数据库架构,所以我不能明确地给您确切的查询。