0

I have a recyclerView called "Notes", I'm trying to add an new recylerview called "Assignments" which will be created inside the notes recylerview item. When I click on a recylerview item (notes) it send me to ClassworkActivity in which the assignments' recyclerview will be added to it. So I want to add the assignment recyclerview as a child of the notes recyclerview.

Here's what I tried:

final AssignmentAdapter assignmentAdapter=new AssignmentAdapter(assignlist,this);
recyclerView.setAdapter(assignmentAdapter);

FloatingActionButton fab = findViewById(R.id.fab);


mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();


String userID = mCurrentUser.getUid();


firebaseDatabase=FirebaseDatabase.getInstance();

databaseReference = firebaseDatabase.getReference().child("Users").child(userID).child("Notes").child(id).child("Assignments");
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
        {
            AssignListdata listdata=dataSnapshot1.getValue(AssignListdata.class);
            assignlist.add(listdata);

        }
        assignmentAdapter.notifyDataSetChanged();

    }

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

    }
});

// fab
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        startActivity(new Intent(getApplicationContext(), AddAssignmentActivity.class));
    }
});

But I'm getting the error on this line:

databaseReference = firebaseDatabase.getReference().
child("Users").
child(userID).
child("Notes").
child(id).
child("Assignments");

since I didn't create the variable id : child(id). which is the id of the Notes List Can anyone tell me how to declare this variable?

Another thing is that the assignments are created as a Notes in the NotesActivity, not in the ClassworkActivity!

JSON tree

Also, this is a github link of my project, Please take a look at it: Notes App

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
xLogger
  • 63
  • 1
  • 7

1 Answers1

0

When you click on recyclerView item of Notes, get the noteID usinng which position is clicked.

now when you open ClassworkActivity, bind that id with intent and get it in ClassworkActivity onCreate() and while creating AssignmentAdapter use that noteID as id.

pass noteId using intent as:

Intent myIntent = new Intent(CurrentActivity.this, ClassworkActivity.class); // UPDATE CURRENT ACTIVITY NAME
myIntent.putExtra("noteID", noteID);
startActivity(myIntent);

and receive in ClassworkActivity onCreate() as:

Intent mIntent = getIntent();
int id = mIntent.getIntExtra("noteID", 0);
Nilesh B
  • 937
  • 1
  • 9
  • 14
  • I tried your solution, but the assignments still created as a note and in the NotesActivity not in the ClassworkActivity – xLogger Apr 06 '20 at 16:56
  • describe your question well, with all possible code – Nilesh B Apr 06 '20 at 16:59
  • Yes it opens when I set itemView.setOnClickListener on the NotesAdapter. I added the JSON tree to understand more my issue and I'll add a github link on the project – xLogger Apr 06 '20 at 16:59
  • in that listener you can obtain which position is clicked, and base on that position you can get note object from list bind in noteAdapter – Nilesh B Apr 06 '20 at 17:01
  • I added a link of my project, can you please take a look ? Much appreciated. Thanks – xLogger Apr 06 '20 at 17:18
  • Is there a turorial for this, because I couldn't find any example. – xLogger Apr 06 '20 at 19:06