[英]Get parent key value on click of child in recyclerview from firebase database
I am creating a recycler view in which, I am getting Survey Name which is child of survey reference number in firebase database.我正在创建一个回收站视图,其中我得到了调查名称,它是 firebase 数据库中调查参考号的子项。 What I want is when user click on survey name in recyclerview list it gets the parent KeyValue (which is survey reference number).
我想要的是当用户单击 recyclerview 列表中的调查名称时,它会获取父 KeyValue(即调查参考号)。 I have attached the image of the database.
我附上了数据库的图像。 On click survey name encircled which blue line, I want the parent key shaded with yellow line
在单击调查名称环绕哪条蓝线时,我希望父键用黄线遮蔽
I have used an Interface in adapter class where I am getting position and modelResponse我在适配器 class 中使用了一个接口,我得到 position 和 modelResponse
public SurveysListAdapter(List<SurveysListModel> modelList, Context context, HandleClickListener handleClickListener)
{
this.modelList = modelList;
this.context = context;
this.handleClickListener = handleClickListener;
}
@Override
public void onBindViewHolder(@NonNull final SurveyListViewHolder holder, final int position) {
final SurveysListModel response = modelList.get(position);
holder.questions.setText(response.getSurvey_name());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClickListener.onItemClicked(position, modelList.get(position));
}
});
}
public interface HandleClickListener {
void onItemClicked(int position, SurveysListModel surveysListModel);
}
Then in Activity Iam getting it like this然后在 Activity 我得到它像这样
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (final DataSnapshot parentSnap : dataSnapshot.getChildren()) {
if (parentSnap.exists()) {
progressDialog.dismiss();
SurveysListModel model = parentSnap.getValue(SurveysListModel.class);
list.add(model);
adapter = new SurveysListAdapter(list, UserAllSurveys.this, new SurveysListAdapter.HandleClickListener() {
@Override
public void onItemClicked(int position, SurveysListModel surveysListModel) {
String typ = surveysListModel.getSurvey_name();
Toast.makeText(UserAllSurveys.this, "CLicked" + position, Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
}
To allow this, you'll need to do two things:为此,您需要做两件事:
parentSnap.getKey()
values available to your adapter, just as you already do for the parentSnap.getValue(...)
objects.parentSnap.getKey()
值,就像您已经为parentSnap.getValue(...)
对象所做的那样。 To make the keys available, you again have two options:要使密钥可用,您还有两个选择:
Add a field for this key to your SurveysListModel
class, and then set that after getting the value.将此键的字段添加到您的
SurveysListModel
class,然后在获取值后进行设置。
Maintain a separate List<String> keyList
in addition to your existing List<SurveysListModel> modelList
and pass both to the constructor of your adapter.除了现有的
List<SurveysListModel> modelList
之外,还维护一个单独的List<String> keyList
并将两者都传递给适配器的构造函数。
For both cases the code will look similar, so I'll just focus on how to get the key and then for example pass it into the `` object (so #1 above):对于这两种情况,代码看起来很相似,所以我将只关注如何获取密钥,然后例如将其传递给 `` object (所以上面的#1):
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (final DataSnapshot parentSnap : dataSnapshot.getChildren()) {
progressDialog.dismiss();
SurveysListModel model = parentSnap.getValue(SurveysListModel.class);
model.key = parentSnap.getKey();
list.add(model);
}
adapter = new SurveysListAdapter(list, UserAllSurveys.this, new SurveysListAdapter.HandleClickListener() {
@Override
public void onItemClicked(int position, SurveysListModel surveysListModel) {
String typ = surveysListModel.getSurvey_name();
String key = surveysListModel.key;
Toast.makeText(UserAllSurveys.this, "CLicked " + key, Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
What I changed here:我在这里改变了什么:
if (parentSnap.exists()) {
, since there is no way for this to be false in a loop over getChildren()
.if (parentSnap.exists()) {
,因为在getChildren()
的循环中,这不可能是错误的。 Pass the key to model.key = parentSnap.getKey();
将密钥传递给
model.key = parentSnap.getKey();
. . You will have to add this
key
field to your class, and probably mark it as @Exclude
to make sure it doesn't get written to the database:您必须将此
key
段添加到 class 中,并可能将其标记为@Exclude
以确保它不会被写入数据库:
@Exclude String key;
Move the whole adapter = new SurveysListAdapter
block outside of the loop over the getChildren()
, which I assume was a copy/paste mistake in your original code.将整个
adapter = new SurveysListAdapter
块移到getChildren()
上的循环之外,我认为这是原始代码中的复制/粘贴错误。
Read they key back from the object with String key = surveysListModel.key;
从 object 中读取他们的密钥,使用
String key = surveysListModel.key;
when the user clicks on an item.当用户点击一个项目时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.