我正在做一个待办事项应用程序,实际上是我的第一个Firebase应用程序。通过创建一个推入ID数组,当在列表视图中单击某个元素时返回推入ID,我已经成功地实现了Firebase UID下的单个文本视图(待办事项)。
这是我以前的数据结构:
我陷入了一个问题,即如何获取元素的推送ID,以便可以到达它们并将其显示在ListView中。
这是我要获取的结构: New data structure
这是我在旧版本中用于添加和删除数据的代码 结构
public void Upload(View v) {
String userId = myRootRef.push().getKey();
firebaseUniqueID =mAuth.getCurrentUser().getUid();
todoToUpload=todoEditText.getText().toString();
myRootRef.child(firebaseUniqueID).child(userId).setValue(todoToUpload);
startActivity(new Intent(AddTodo.this,showTodo.class));
这是我用来添加和删除的代码:
FirebaseAuth mAuth;
FirebaseDatabase mDataBase= FirebaseDatabase.getInstance();
DatabaseReference myRootRef=mDataBase.getReference();
DatabaseReference userRef;
ListView listView;int pos;
private ArrayAdapter<String> adapter;
ArrayList<String> mTodo=new ArrayList<>();
ArrayList<String> keysList = new ArrayList<>();
adapter=new ArrayAdapter<String>(this,R.layout.custom_list_layout,R.id.tood,mTodo);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos=position;
AlertDialog.Builder mBuilder=new AlertDialog.Builder(showTodo.this);
View mView=getLayoutInflater().inflate(R.layout.custom_dialog_show,null);
Todo=mView.findViewById(R.id.todo);
Delete=mView.findViewById(R.id.delete);
Done=mView.findViewById(R.id.done);
closer=mView.findViewById(R.id.dialog_close);
mBuilder.setView(mView);
final AlertDialog dailog=mBuilder.create();
myRootRef.child(firebaseUniqueID).child(keysList.get(position)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
todoOpened=dataSnapshot.getValue(String.class);
Todo.setText(todoOpened);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Done.isChecked())
{
mTodo.remove(pos);
adapter.notifyDataSetChanged();
//new code below
myRootRef.getRoot().child(firebaseUniqueID).child(keysList.get(pos)).removeValue();
keysList.remove(pos);
dailog.dismiss();
}
}
});
myRootRef.child(firebaseUniqueID)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot firebaseUniqueID : dataSnapshot.getChildren()) {
keysList.add(firebaseUniqueID.getKey());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
/*handle errors*/
}
});
userRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value= dataSnapshot.getValue(String.class);
mTodo.add(value);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是旧结构和新结构的JSON文件
{
"Z2H2ZkX56fYv3WKxE3a3nCaa8Q63" : {
"-LNPyZIkX8-uDDGeX-pN" : {
"Time" : "22:56",
"Todo" : "play"
},
"-LNPze7ppG1L0qFukZO-" : {
"Priority" : "High",
"Time" : "22:56",
"TimeSet" : "09:56",
"Todo" : "play till the sun goes down"
},
"-LNPzhBDDtHv_XSIERX_" : {
"Priority" : "High",
"Time" : "22:56",
"TimeSet" : "09:56",
"Todo" : "play till the sun goes ddawdawdawdwown"
}
}
}
{
"Z2H2ZkX56fYv3WKxE3a3nCaa8Q63" : {
"-LNQRCZSsYVPqn0tWghZ" : "play in the evening",
"-LNQRrLliVSxWBHcEIrv" : "I want to shine like a sun"
}
}
答案 0 :(得分:0)
您可以通过调用DataSnapshot
方法来获取getKey()
的密钥。例如:
public void onDataChange(DataSnapshot dataSnapshot) {
todoOpened=dataSnapshot.getValue(String.class);
String key = dataSnapshot.getKey();
Todo.setText(key + ": "+ todoOpened);
}
答案 1 :(得分:0)
您可以按照在{https://i.stack.imgur.com/E4DXI.png
中显示的新结构来显示列表。 FirebaseRecyclerOptions<Messages> options =
new FirebaseRecyclerOptions.Builder<"Your Class Name">()
.setQuery(mMessageThread.child("messages").limitToLast(500), "Your Class Name".class)
.setLifecycleOwner(this)
.build();
与firebase适配器一起使用。
然后获取ToDo文本并显示。