我如何从 Firebase 检索密钥并将其映射到它的模型实例
How can i retrieve keys from Firebase and map it to its model instance
我如何检索我的数据库条目的键并将它们映射到我的 java 对象,以便我可以更新数据库中每个单独对象的内容?我想检索下面 JSON 中表示的 "TODO KEY"。
数据库
{
"todos" : {
"7Y3s5KPhyLU8mR7cbUGNbcnNQ7f1 (USER KEY)" : {
"-LCddfFC048CowqkTfV1 (TODO KEY)" : {
"description" : "vhh",
"title" : "hh"
},
"-LCdiAkltHn3Zs47Fdyg" : {
"description" : "hg5hh",
"title" : "bcgyi"
}
}
}
}
Android代码
mUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mQuery = FirebaseDatabase.getInstance().getReference().child("todos/" + mUserId);
//Initialize RecyclerView
mRecyclerView = findViewById(R.id.recyclerView);
//Set layout manager for RecyclerView
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Initialize options for adapter
FirebaseRecyclerOptions<Todo> options =
new FirebaseRecyclerOptions.Builder<Todo>()
.setQuery(mQuery, Todo.class)
.build();
//Initialize adapter
mAdapter = new FirebaseRecyclerAdapter<Todo, TodoHolder>(options) {
@Override
public TodoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new TodoHolder(view);
}
@Override
protected void onBindViewHolder(TodoHolder holder, int position, Todo todo) {
// Bind the Chat object to the ChatHolder
holder.bind(todo);
}
};
//Connect adapter to recyclerview
mRecyclerView.setAdapter(mAdapter);
要获取 onBindViewHolder
中的密钥,您可以根据 position
:
从适配器中查找它
DatabaseReference ref = mAdapter.getReference(position);
String key = ref.getKey();
我如何检索我的数据库条目的键并将它们映射到我的 java 对象,以便我可以更新数据库中每个单独对象的内容?我想检索下面 JSON 中表示的 "TODO KEY"。
数据库
{
"todos" : {
"7Y3s5KPhyLU8mR7cbUGNbcnNQ7f1 (USER KEY)" : {
"-LCddfFC048CowqkTfV1 (TODO KEY)" : {
"description" : "vhh",
"title" : "hh"
},
"-LCdiAkltHn3Zs47Fdyg" : {
"description" : "hg5hh",
"title" : "bcgyi"
}
}
}
}
Android代码
mUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mQuery = FirebaseDatabase.getInstance().getReference().child("todos/" + mUserId);
//Initialize RecyclerView
mRecyclerView = findViewById(R.id.recyclerView);
//Set layout manager for RecyclerView
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Initialize options for adapter
FirebaseRecyclerOptions<Todo> options =
new FirebaseRecyclerOptions.Builder<Todo>()
.setQuery(mQuery, Todo.class)
.build();
//Initialize adapter
mAdapter = new FirebaseRecyclerAdapter<Todo, TodoHolder>(options) {
@Override
public TodoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new TodoHolder(view);
}
@Override
protected void onBindViewHolder(TodoHolder holder, int position, Todo todo) {
// Bind the Chat object to the ChatHolder
holder.bind(todo);
}
};
//Connect adapter to recyclerview
mRecyclerView.setAdapter(mAdapter);
要获取 onBindViewHolder
中的密钥,您可以根据 position
:
DatabaseReference ref = mAdapter.getReference(position);
String key = ref.getKey();