Get parent key value on click of child in recyclerview from firebase database

老子叫甜甜 提交于 2021-01-29 12:08:47

问题


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:

  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.



来源:https://stackoverflow.com/questions/58731376/get-parent-key-value-on-click-of-child-in-recyclerview-from-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!