1

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

enter image description here

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);
                    }
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38

1 Answers1

3

To allow this, you'll need to do two things:

  1. Make the parentSnap.getKey() values available to your adapter, just as you already do for the parentSnap.getValue(...) objects.
  2. 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:

  1. Add a field for this key to your SurveysListModel class, and then set that after getting the value.

  2. Maintain a separate List<String> keyList in addition to your existing List<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:

  1. Remove the if (parentSnap.exists()) {, since there is no way for this to be false in a loop over getChildren().
  2. Pass the key to 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:

    @Exclude 
    String key;
    
  3. 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.

  4. Read they key back from the object with String key = surveysListModel.key; when the user clicks on an item.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I didnt understand what changes should I do in adapter and model class can you elaborate a little more please – Umair Iqbal Nov 06 '19 at 15:12
  • For the first solution, which is what I elaborated on in the rest of the answer, you'll need to add a field to your `SurveysListModel` matching the #2 step in the last list. The rest of the change is in the big `onDataChange` code block in my answer. – Frank van Puffelen Nov 06 '19 at 15:23
  • what is model.key whixh you have written in onDataChange – Umair Iqbal Nov 06 '19 at 15:37
  • It adds the **key** of the snapshot to the `SurveysListModel` that you already have, so that you have both key and values in one place. – Frank van Puffelen Nov 06 '19 at 17:01