从firebase数据库中获取recyclerview中子项单击时的父键值 [英] Get parent key value on click of child in recyclerview from firebase database
问题描述
我正在创建一个回收者视图,在其中,我得到的调查名称是Firebase数据库中调查参考编号的子代.我想要的是,当用户在recyclerview列表中单击调查名称时,它将获得父KeyValue(这是调查参考编号).我已附上数据库的图像.在点击调查名称用蓝色线围起来时,我希望父键用黄色线阴影
I am creating a recycler view in which, I am getting Survey Name which is child of survey reference number in firebase database. What I want is when user click on survey name in recyclerview list it gets the parent KeyValue (which is survey reference number). 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
我在适配器类中使用了一个接口,在那里我要获取位置和modelResponse
I have used an Interface in adapter class where I am getting position and 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);
}
然后在Activity Iam中获得它
Then in Activity Iam getting it like this
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()
值可用于适配器,就像对parentSnap.getValue(...)
对象所做的一样. - 通常通过按位置查找来查找用户单击的值的正确键.
- Make the
parentSnap.getKey()
values available to your adapter, just as you already do for theparentSnap.getValue(...)
objects. - Look up the correct key for the value the user has clicked on, typically by looking it up by its position.
要使密钥可用,您再次有两个选择:
To make the keys available, you again have two options:
-
为此键的一个字段添加到您的
SurveysListModel
类,然后在获取值后进行设置.
Add a field for this key to your
SurveysListModel
class, and then set that after getting the value.
维护一个单独的 List< String>除了现有的
并将它们都传递给适配器的构造函数. List< SurveysListModel> keyList
modelList
Maintain a separate List<String> keyList
in addition to your existing List<SurveysListModel> modelList
and pass both to the constructor of your adapter.
对于这两种情况,代码看起来都是相似的,所以我只着重于如何获取密钥,然后例如将其传递给``对象(上面的#1):
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):
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);
我在这里所做的更改:
- 删除
if(parentSnap.exists()){
,因为在getChildren()
的循环中无法将其设为false. -
将密钥传递给
model.key = parentSnap.getKey();
.您将必须将此key
字段添加到您的类中,并可能将其标记为@Exclude
,以确保它不会被写入数据库:
- Remove the
if (parentSnap.exists()) {
, since there is no way for this to be false in a loop overgetChildren()
. Pass the key to
model.key = parentSnap.getKey();
. You will have to add thiskey
field to your class, and probably mark it as@Exclude
to make sure it doesn't get written to the database:
@Exclude
String key;
将整个 adapter = new SurveysListAdapter
块移到 getChildren()
的循环外,我认为这是您原始文档中的复制/粘贴错误代码.
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.
当用户单击某个项目时,使用 String key = surveysListModel.key;
从对象读取它们的密钥.
Read they key back from the object with String key = surveysListModel.key;
when the user clicks on an item.
这篇关于从firebase数据库中获取recyclerview中子项单击时的父键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!