使用hashmap将电子邮件保存到firebase数据库中

时间:2017-04-15 15:07:34

标签: java android firebase-realtime-database

好的,所以每当他/她点击列表视图中的某个名称时,我都希望将用户的电子邮件存储到我的数据库中。当然,电子邮件将根据用户点击的名称进行存储。但是我收到了这个错误,而且我对使用hashmap知之甚少。那么有人可以帮我解决这个问题吗?

MainActivity:

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());
 FirebaseUser user = firebaseAuth.getCurrentUser();

final String teamMember = user.getEmail();
            final String name = team.getTeamName();
            final String encodedEmailAddress = encodeUserEmail(teamMember);
            databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(!dataSnapshot.child("members").child(name).child("teamMember").hasChild(encodedEmailAddress)){
                        Map<String,Object> map = new HashMap<>();
                        map.put("member",encodedEmailAddress);
                        databaseMembers.child(name).child("teamMember").updateChildren(map);
                        //databaseMembers.child(name).child("teamMember").setValue(encodedEmailAddress);

                    }
                    else{

                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            //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);
        }
    });

static String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}
static String decodeUserEmail(String userEmail) {
    return userEmail.replace(",", ".");
}

Member.java

public class Member {
private String teamMember;


public Member(){

}

public Member(String teamMember){
    this.teamMember = teamMember;
}

public String getTeamMember() {return teamMember;}
public void setTeamMember(String teamMember) {this.teamMember = 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; }

}

数据库:

enter image description here

logcat的:

04-15 14:41:47.914 2483-2483/com.example.user.stfv2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.example.user.stfv2, PID: 2483
                                                                  com.google.firebase.database.DatabaseException: Maps with non-string keys are not supported
                                                                      at com.google.android.gms.internal.zzbtg.zzaz(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbtg.zzaG(Unknown Source)
                                                                      at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
                                                                      at com.google.firebase.database.DatabaseReference.updateChildren(Unknown Source)
                                                                      at com.example.user.stfv2.MainActivity$8.onDataChange(MainActivity.java:372)
                                                                      at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

如果缺乏信息,请告诉我。谢谢。

编辑: 好的,现在我已经更改了我的代码,我删除了checkMember()函数并直接将代码放在onClick中。所以现在我可以成功地将当前用户的电子邮件作为团队成员之一插入到数据库中,如我更新的数据库中所示。现在的问题是,如果数据库中不存在电子邮件,我的代码将替换成员而不是添加。所以我仍然坚持无法添加成员,是因为名称&#34;成员&#34;不能在数据库中重复?请注意错误已消失,请忽略它。

0 个答案:

没有答案