ThePeskyWabbit ThePeskyWabbit - 18 days ago 9
Android Question

Trying to copy data from one place in firebase to another. Getting database path error

Here is my current database structure:

{
"Classes" : {
"aa" : {
"classID" : "aa",
"className" : "aa",
"days" : [ "tuesday", "thursday" ],
"startTime" : "1010",
"teacherID" : "XiYeok0F0zMtZT7qRVeEXRilWEs1"
},
"test101" : {
"classID" : "test101",
"className" : "testing classes",
"days" : [ "monday", "wednesday", "friday" ],
"gpsLocation" : "999",
"startTime" : "1010",
"teacherID" : "y4cLhvIGvkcErHPTAXsglb0rXcl1"
}
},
"Users" : {
"jYi10gDlWEcoLgJ6xgRHQ2qeAxI2" : {
"classList" : [ {
"className" : "Ignore me"} ],
"fname" : "Rick",
"id" : "jYi10gDlWEcoLgJ6xgRHQ2qeAxI2",
"lname" : "Ross"
},
"reg6pCXft3Uvlg87VxXFnEBQ6Lt1" : {
"classList" : [ {
"className" : "Ignore me"} ],
"fname" : "name",
"id" : "reg6pCXft3Uvlg87VxXFnEBQ6Lt1",
"lname" : "name"
},
"y4cLhvIGvkcErHPTAXsglb0rXcl1" : {
"classList" : [ {
"className" : "Ignore me"} ],
"fname" : "name",
"id" : "y4cLhvIGvkcErHPTAXsglb0rXcl1",
"lname" : "name"
}
}
}


What I am trying to do is copy the user that is open in the picture, and paste that information with the same formatting into the class "test101" but in a node called roster.

I have seen plenty with value change listeners but I dont see how that will apply here if i am not changing the values but just need to read them into code and then put them elsewhere.

here is my code so far:

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class Join_Class extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_join_class);
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference fbRef = FirebaseDatabase.getInstance().getReference();

final EditText etClassID = (EditText) findViewById(R.id.etClassID);
Button joinBut = (Button) findViewById(R.id.bJoin);
final String userID = fbRef.child("Users").child(user.getUid()).getKey();


joinBut.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
final String classID = etClassID.getText().toString().trim();
if(fbRef.child("Classes").child(classID).getKey() == classID){
//get user id node information
//put into arraylist in class specified
//not asking you to write this for me, maybe what to use
//for the task
}
sendTo(Main__Menu.class);
}
});

}

private void sendTo(Class s){
Intent intent = new Intent(this, s);
startActivity(intent);
}
}

Answer

According to your code in OnClickListenr

fbRef.child("Classes").child(classID).getKey()

In my opinion, the proper way to do your query is:

// Filter the object (Classes) you would like to deal with
Query query = fbRef.child("Classes").orderByChild(classID).equalTo(classID);

// Then add a listener
query.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                // classID matched, do your stuffs
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
  });

Hope my answer can help you