这是我目前的数据库结构:
{
"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"
}
}
}
我要做的是复制图片中打开的用户,并将具有相同格式的信息粘贴到“test101”类中,但是在名为roster的节点中。
我已经看到很多有价值变化的听众,但我不知道如果我不改变价值而只需要将它们读入代码然后将它们放在其他地方,这将如何应用。
到目前为止,这是我的代码:
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);
}
}
答案 0 :(得分:0)
根据您在OnClickListenr中的代码
fbRef.child("Classes").child(classID).getKey()
在我看来,执行查询的正确方法是:
// 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) {
}
});
希望我的回答可以帮到你