Umair Iqbal
Umair Iqbal

Reputation: 2069

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. 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);
                    }

Upvotes: 1

Views: 1463

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

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.

Upvotes: 3

Related Questions