问题
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
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);
}
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);
}
回答1:
To allow this, you'll need to do two things:
- 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:
Add a field for this key to your
SurveysListModel
class, and then set that after getting the value.Maintain a separate
List<String> keyList
in addition to your existingList<SurveysListModel> modelList
and pass both to the constructor of your adapter.
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);
What I changed here:
- 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;
Move the whole
adapter = new SurveysListAdapter
block outside of the loop over thegetChildren()
, which I assume was a copy/paste mistake in your original code.Read they key back from the object with
String key = surveysListModel.key;
when the user clicks on an item.
来源:https://stackoverflow.com/questions/58731376/get-parent-key-value-on-click-of-child-in-recyclerview-from-firebase-database