简体   繁体   English

从 firebase 数据库中获取在 recyclerview 中单击子项的父键值

[英]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.我正在创建一个回收站视图,其中我得到了调查名称,它是 firebase 数据库中调查参考号的子项。 What I want is when user click on survey name in recyclerview list it gets the parent KeyValue (which is survey reference number).我想要的是当用户单击 recyclerview 列表中的调查名称时,它会获取父 KeyValue(即调查参考号)。 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我在适配器 class 中使用了一个接口,我得到 position 和 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然后在 Activity 我得到它像这样

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:为此,您需要做两件事:

  1. Make the parentSnap.getKey() values available to your adapter, just as you already do for the parentSnap.getValue(...) objects.使您的适配器可以使用parentSnap.getKey()值,就像您已经为parentSnap.getValue(...)对象所做的那样。
  2. Look up the correct key for the value the user has clicked on, typically by looking it up by its position.查找用户单击的值的正确键,通常通过其 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.将此键的字段添加到您的SurveysListModel class,然后在获取值后进行设置。

  2. Maintain a separate List<String> keyList in addition to your existing List<SurveysListModel> modelList and pass both to the constructor of your adapter.除了现有的List<SurveysListModel> modelList之外,还维护一个单独的List<String> keyList并将两者都传递给适配器的构造函数。

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):对于这两种情况,代码看起来很相似,所以我将只关注如何获取密钥,然后例如将其传递给 `` object (所以上面的#1):

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() .删除if (parentSnap.exists()) { ,因为在getChildren()的循环中,这不可能是错误的。
  2. Pass the key to model.key = parentSnap.getKey();将密钥传递给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:您必须将此key段添加到 class 中,并可能将其标记为@Exclude以确保它不会被写入数据库:

     @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.将整个adapter = new SurveysListAdapter块移到getChildren()上的循环之外,我认为这是原始代码中的复制/粘贴错误。

  4. Read they key back from the object with String key = surveysListModel.key;从 object 中读取他们的密钥,使用String key = surveysListModel.key; when the user clicks on an item.当用户点击一个项目时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击被推送到Firebase的recyclerview项目时,如何获取Firebase子键值 - How can i get firebase child key value when i click on recyclerview item which is pushed to firebase 试图从 Firebase 数据库中的子值获取密钥 - Trying To Get Key From a Child Value In Firebase Database 如何从Firebase运行时数据库获取特定的子键并在RecyclerView Android上设置数据 - How to get particular child key from firebase runtime database and set data on recyclerview android 是否可以在按钮单击时从 android 更新 firebase 实时数据库中没有 ID 或键的特定孩子的值? - Is it possible to update a specific child's value without ID or key in firebase realtime database from android on button click? 基于当前用户 Firebase Android 的父子项获取键的值 - Get Value of Key Based on the Parent Child of Current User Firebase Android 使用Firebase UI在recyclerView中显示时如何从firebase数据库中的子项引用父项 - How to reference to the parent from child in firebase database while showing in recyclerView usin Firebase UI 如何从 Firebase 实时数据库中的子节点获取父值? - How do I get the parent value from child in Firebase real time database? 如何从其子 recyclerview 视图中获取父 recyclerview 的引用? - How to get reference of the parent recyclerview from its child recyclerview views? 如何使用RecyclerView从Firebase获取密钥 - How to get key from firebase using RecyclerView 父级RecyclerView单击事件的子级RecyclerView项 - Items of Child RecyclerView of parent RecyclerView click event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM