简体   繁体   中英

Trying to get key from firebase, instead getting a weird path

I have a database structure like this:

"users": {
   "school": {
      userId (randomkey) {
         "email": "email@emai.email"
         "provider": "provider"
      }
   }
}

I'm using a recycler view where users can add each other to a group. I'm showing the email for the user in the recycler view and that works fine. But the problem is that I need to retrieve the userId key for the email that is clicked on and append that to a List that I then push to firebase. I'm doing all that with this code.

Adapter class

    class RecyclerViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {

    private ItemClickListenerPeopleToAdd itemClickListenerPeopleToAdd;
    public TextView emailLbl;

    public RecyclerViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        emailLbl = (TextView) itemView.findViewById(R.id.emailLbl);
    }

    public void setItemClickListenerPeopleToAdd(ItemClickListenerPeopleToAdd itemClickListenerPeopleToAdd) {
        this.itemClickListenerPeopleToAdd = itemClickListenerPeopleToAdd;
    }

    @Override
    public void onClick(View v) {

        itemClickListenerPeopleToAdd.onClick(v, getAdapterPosition());
    }
}

public class PeopleToAddAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
    private static final String TAG = "Working";
    public ArrayList<String> peopleList = new ArrayList<String>();
    private List<ModelProject> mModelList;

    public PeopleToAddAdapter(List<ModelProject> modelList) {
        mModelList = modelList;
    }

    private Context context;
    private List<PeopleToAdd> mPeopleToAdd;
    public List<PeopleToAdd> mList;


    public PeopleToAddAdapter(Context con, List<PeopleToAdd> list) {
        this.context = con;
        this.mPeopleToAdd = list;
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.people_to_add_listview, parent, false);

        return new RecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {

        final PeopleToAdd listItem = mPeopleToAdd.get(position);
        holder.emailLbl.setText(listItem.getEmail());
        holder.setItemClickListenerPeopleToAdd(new ItemClickListenerPeopleToAdd() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(context, "Click" + listItem.getEmail(), Toast.LENGTH_SHORT).show();
                peopleList.add(listItem.toString());
                Log.d("value", String.valueOf(peopleList));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mPeopleToAdd.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView emailLbl;

        public MyViewHolder(View itemView) {
            super(itemView);
            emailLbl = (TextView) itemView.findViewById(R.id.emailLbl);
        }
    }
}

PeopleToAdd

public class PeopleToAdd {
private String email;
private String provider;

public PeopleToAdd(String email, String provider) {
    this.email = email;
    this.provider = provider;
}

public PeopleToAdd() {

}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getProvider() {
    return provider;
}

public void setProvider(String provider) {
    this.provider = provider;
}

}

Activity class

listItems = new ArrayList<>();
    schoolList = new ArrayList<>();

    adapter = new PeopleToAddAdapter(this, listItems);
    recyclerView.setAdapter(adapter);

    mGetSchool = mDatabase.getReference().child("users").child(mCurrentUserId).child("schoolName");

protected void onStart() {
    super.onStart();

    mGetSchool.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            childPath = dataSnapshot.getValue(String.class);
            //peopleAddedTxtView.setText(childPath);
            mPeopleToAdd = mDatabase.getReference().child("users").child(childPath);

            mPeopleToAdd.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                    PeopleToAdd peopleToAdd = dataSnapshot.getValue(PeopleToAdd.class);
                    listItems.add(peopleToAdd);

                    test = dataSnapshot.getKey();
                    Log.d(TAG, test);
                    peopleToAddAdapter = new PeopleToAddAdapter(SetProject.this, listItems);
                    recyclerView.setAdapter(peopleToAddAdapter);
                    peopleToAddAdapter.notifyDataSetChanged();
                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

I'm somehow getting a strange path as the userID example: "com.appName.Services.PeopleToAdd@ca8f01". How can I get the right userId (the random key generated by firebase). I have it in the "test" String in activity but how can I add that to the List when a user clicks on that user in the reyclerView. I hope you understand me. Thank you in advance.

A snapshot contains two main things: the key from where the data was loaded, and the value that was loaded. Right now your onChildAdded only uses the value, and throws the key away:

public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

    PeopleToAdd peopleToAdd = dataSnapshot.getValue(PeopleToAdd.class);
    listItems.add(peopleToAdd);

    test = dataSnapshot.getKey();
    Log.d(TAG, test);
    peopleToAddAdapter = new PeopleToAddAdapter(SetProject.this, listItems);
    recyclerView.setAdapter(peopleToAddAdapter);
    peopleToAddAdapter.notifyDataSetChanged();
}

And since you need the key later in your code, you can't retrieve it anymore. The value doesn't contain the key.

One solution is to add the key to you PeopleToAdd class. Based on the answer I gave to Is there a way to store Key in class which I cast from Firebase object? , that could look like this for you:

public class PeopleToAdd {
    @Exclude
    public String key;

    private String email;
    private String provider;

    public PeopleToAdd(String email, String provider) {
        this.email = email;
        this.provider = provider;
    }

    public PeopleToAdd() {

    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getProvider() {
        return provider;
    }

    public void setProvider(String provider) {
        this.provider = provider;
    }
}

And then modify your onChildAdded to set the key on the PeopleToAdd it instantiated from the value:

public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

    PeopleToAdd peopleToAdd = dataSnapshot.getValue(PeopleToAdd.class);

    peopleToAdd.key = dataSnapshot.getKey();

    listItems.add(peopleToAdd);

    peopleToAddAdapter = new PeopleToAddAdapter(SetProject.this, listItems);
    recyclerView.setAdapter(peopleToAddAdapter);
    peopleToAddAdapter.notifyDataSetChanged();
}

Now you can get the key of the PeopleToAdd from its key field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM