我的问题如上所述,我添加的数据没有存储在正确的孩子身上。如果数据存储在错误的孩子身上,这意味着检查孩子是否存在的部分将不会发生。
EditText editTextName;
EditText textDate;
EditText textTime;
Spinner spinnerSport;
ListView listViewTeams;
DatePickerDialog datePickerDialog;
//a list to store all the team from firebase database
List<Team> teams;
List<Member> members;
//our database reference object
DatabaseReference databaseTeams;
DatabaseReference databaseMembers;
databaseTeams = FirebaseDatabase.getInstance().getReference("teams");
databaseMembers = FirebaseDatabase.getInstance().getReference("members");
//getting views
editTextName = (EditText) findViewById(R.id.editTextName);
spinnerSport = (Spinner) findViewById(R.id.spinnerSports);
listViewTeams = (ListView) findViewById(R.id.listViewTeams);
textDate = (EditText) findViewById(R.id.textDate);
textTime = (EditText) findViewById(R.id.textTime);
//list to store teams
teams = new ArrayList<>();
//attaching listener to listview
listViewTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//getting the selected team
Team team = teams.get(i);
checkMember();
//creating an intent
Intent intent = new Intent(getApplicationContext(), ChatRoomActivity.class);
//putting team name and id to intent
intent.putExtra(TEAM_ID, team.getTeamId());
intent.putExtra(TEAM_NAME, team.getTeamName());
//starting the activity with intent
startActivity(intent);
}
});
@Override
protected void onStart() {
super.onStart();
//attaching value event listener
databaseTeams.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//clearing the previous team list
teams.clear();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting team
Team team = postSnapshot.getValue(Team.class);
//adding team to the list
teams.add(team);
}
//creating adapter
TeamList teamAdapter = new TeamList(MainActivity.this, teams);
//attaching adapter to the listview
listViewTeams.setAdapter(teamAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void checkMember(){
FirebaseUser user = firebaseAuth.getCurrentUser();
final String teamMember = user.getEmail();
final String encodedEmailAddress = encodeUserEmail(teamMember);
databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(encodedEmailAddress)){
finish();
}
else{
databaseMembers.push().setValue(encodedEmailAddress);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
该电子邮件已添加到“成员”中,但我希望将其添加到用户在列表视图中单击的团队名称中,以便将电子邮件存储到其中,例如说“heng”。任何帮助表示赞赏。如果缺少信息,请告诉我。
编辑2: 这是我目前的checkMember,
private void checkMember(final String teamName){
FirebaseUser user = firebaseAuth.getCurrentUser();
final Member memberObject = new Member();
final String teamMember = user.getEmail();
final String encodedEmailAddress = encodeUserEmail(teamMember);
databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.child("members").child(teamName).hasChild("teamMember")){
Team team = dataSnapshot.getValue(Team.class);
memberObject.setTeamMember("teamMember");
Map<String,Object> map = new HashMap<>();
map.put(Team.getTeamName(),memberObject);
databaseMembers.child(teamName).updateChildren(map);
databaseMembers.child(teamName).child("teamMember").setValue(encodedEmailAddress);
}
else{
}
}
Member.java
public class Member {
private String teamMember;
public Member(){
}
public Member(String teamMember){
this.teamMember = teamMember;
}
public String getTeamMember() {return teamMember;}
public String setTeamMember(String teamMember) {return this.teamMember;}
}
Team.java
public class Team {
private String teamId;
private String teamName;
private String teamSport;
private String teamOwner;
private String date;
private String time;
public Team() {
}
public Team(String teamId, String teamName, String teamSport, String teamOwner, String date, String time) {
this.teamId = teamId;
this.teamName = teamName;
this.teamSport = teamSport;
this.teamOwner = teamOwner;
this.date = date;
this.time = time;
}
public String getTeamId() {return teamId;}
public String getTeamName() {
return teamName;
}
public String getTeamSport() {
return teamSport;
}
public String getTeamOwner() { return teamOwner; }
public String getDate(){ return date; }
public String getTime() { return time; }
}
答案 0 :(得分:0)
为了做到这一点,您需要从点击的ListView
项中获取团队名称。拥有team name
,您就可以在新的DatabaseReference
中使用它,以便在您想要的确切位置添加电子邮件地址。因此,假设您有一个活动,您可以使用ListView
使用FirebaseListAdapter
数据库中的所有团队来显示Teams
。此部分名为 ListView listView = (ListView) findViewById(R.id.list_view);
DatabaseReference ref = databaseMembers.child("Teams");
FirebaseListAdapter firebaseListAdapter = new FirebaseListAdapter<String>(YourActivity.this, String.class, R.layout.list) {
@Override
protected void populateView(final View v, final String teamName, int position) {
((TextView )v.findViewById(R.id.team_name)).setText(teamName);
}
};
listView.setAdapter(firebaseListAdapter);
。
adapter
设置setOnItemClickListener
后,我们需要 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String teamName = (String) adapterView.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), YourNewActivity.class);
intent.putExtra("teamName", teamName);
startActivity(intent);
}
});
查看列表中的哪个元素,用户点击了该元素。我们将元素存储在intent中,之后我们将能够在第二个活动中使用它。
teamName
最后,要从第二个活动中获取String teamName = (String) getIntent().getExtras().get("teamName");
,我们将使用此代码:
<Router history={browserHistory}>
<Route path="/" component={Root}>
<IndexRoute component={App} />
<Route path="/sna/:country" component={SNA} />
<Route path="*" component={FourOFour}/>
</Route>
</Router>
希望它有所帮助。
答案 1 :(得分:0)
不要使用push()它总是在firebase中创建一个新的子键,试试这个并让我知道这是否有效
databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.child("members").child("your groupname").hasChild("teamMember")){//do your check for here
databaseMembers.child("members").child("your groupname").child("teamMember").setValue(encodedEmailAddress);
}
else{
finish();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
更新1:
private void checkMember(String teamName){
FirebaseUser user = firebaseAuth.getCurrentUser();
final String teamMember = user.getEmail();
final String encodedEmailAddress = encodeUserEmail(teamMember);
databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.child("members").child(teamName).hasChild("teamMember")){//do your check for here
databaseMembers.child("members").child(teamName).child("teamMember").setValue(encodedEmailAddress);
}
else{
finish();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//attaching listener to listview
listViewTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//getting the selected team
Team team = teams.get(i);
checkMember(team.getTeamName());//pass your team name here
//creating an intent
Intent intent = new Intent(getApplicationContext(), ChatRoomActivity.class);
//putting team name and id to intent
intent.putExtra(TEAM_ID, team.getTeamId());
intent.putExtra(TEAM_NAME, team.getTeamName());
//starting the activity with intent
startActivity(intent);
}
});
<强> UPDATE2:强>
替换现有的孩子
Member memberObject = new Member();
memberObject.setTeamMember("your email here");//edit your object
Map<String,Object> map = new HashMap<>();
map.put(team.getTeamName(),memberObject);
firebaseDatabase.getReference().databaseMembers.child("members").child(teamName).updateChildren(map);