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.