我正在做一个项目,User
充当经理可以创建Team
并加入它。所有团队名称都使用ArrayAdapter
显示在列表中。我可以显示所有Team names
,AFL1
,AFL2
,AHL1
,AHL3
,当User
选择一个时,我会添加到Team
User
和User
对象。当我尝试显示AHL1
'经理加入的所有小组时,AHL2
和02-07 19:23:19.300 9813-9813/com.example.cianm.testauth E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cianm.testauth, PID: 9813
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
at com.example.cianm.testauth.SelectTeam.collectTeamNames(SelectTeam.java:89)
at com.example.cianm.testauth.SelectTeam.access$000(SelectTeam.java:28)
at com.example.cianm.testauth.SelectTeam$1.onDataChange(SelectTeam.java:62)
at com.google.firebase.database.zzp.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzegf.zza(Unknown Source)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
at com.google.android.gms.internal.zzeig.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
我收到以下错误:
Map singleTeam = (Map) entry.getValue();
以下是我的代码的第89行:
package com.example.cianm.testauth;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.cianm.testauth.Entity.CurrentTeam;
import com.example.cianm.testauth.Entity.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Map;
public class SelectTeam extends AppCompatActivity {
private DatabaseReference mDatabaseT, mDatabaseU;
private FirebaseAuth mAuth;
private FirebaseUser fbUser;
private Query mDatabaseQuery;
ListView lv;
Button mJoinTeam;
ProgressBar mProgressBar;
User user;
String teamID, userType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_team);
lv = (ListView) findViewById(R.id.selectTeamListView);
mJoinTeam = (Button) findViewById(R.id.joinTeamBtn);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.GONE);
mDatabaseT = FirebaseDatabase.getInstance().getReference("Team");
mDatabaseU = FirebaseDatabase.getInstance().getReference("User");
mAuth = FirebaseAuth.getInstance();
fbUser = mAuth.getCurrentUser();
mDatabaseQuery = FirebaseDatabase.getInstance().getReference("User").child(fbUser.getUid()).child("team");
mDatabaseQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
collectTeamNames((Map<String, Object>) dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mJoinTeam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(SelectTeam.this, JoinTeam.class));
}
});
}
private void collectTeamNames(Map<String, Object> team){
String teamKey = mDatabaseU.child("team").push().getKey();
final ArrayList<String> teamNames = new ArrayList<>();
for (Map.Entry<String, Object> entry : team.entrySet()){
Map singleTeam = (Map) entry.getValue();
teamNames.add((String) singleTeam.get(teamKey));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SelectTeam.this, android.R.layout.select_dialog_singlechoice, teamNames);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mProgressBar.setVisibility(View.VISIBLE);
mJoinTeam.setVisibility(View.GONE);
teamID = adapterView.getItemAtPosition(i).toString();
mDatabaseU.child(fbUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
userType = user.getType();
((CurrentTeam) SelectTeam.this.getApplication()).setCurrentTeam(teamID);
if (user.getType().equalsIgnoreCase("Manager")) {
startActivity(new Intent(SelectTeam.this, ManagerHome.class));
} else if (user.getType().equalsIgnoreCase("Player")){
startActivity(new Intent(SelectTeam.this, PlayerHome.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Toast.makeText(SelectTeam.this,"Loading data for team " + teamID, Toast.LENGTH_SHORT).show();
// startActivity(new Intent(SelectTeam.this, ManagerHome.class));
}
});
}
}
以下是我的完整代码,其中包含错误:
{
"Team" : {
"AFL1" : {
"division" : "1",
"name" : "AFL1",
"type" : "Football"
},
"AFL2" : {
"division" : "2",
"manager" : {
"VoDClWm871fsIX6KO5SfRmjlGEf2" : "Cian McGrane "
},
"name" : "AFL2",
"type" : "Football"
},
"AHL1" : {
"division" : "1",
"manager" : {
"VoDClWm871fsIX6KO5SfRmjlGEf2" : "Cian McGrane "
},
"name" : "AHL1",
"type" : "Hurling"
},
"AHL3" : {
"division" : "3",
"manager" : {
"VoDClWm871fsIX6KO5SfRmjlGEf2" : "Cian McGrane "
},
"name" : "AHL3",
"type" : "Hurling"
}
},
"User" : {
"VoDClWm871fsIX6KO5SfRmjlGEf2" : {
"email" : "cianmcgrane@gmail.com",
"name" : "Cian McGrane ",
"password" : "password",
"team" : {
"-L4gns3cyu96k3XN8rjl" : "AHL1",
"-L4gnw1VMco0sefy8-g0" : "AFL2"
},
"type" : "Manager"
}
}
}
这是我的数据库:
User
我想显示列表中InvalidOperationException: Incorrect Content-Type:
对象中的所有团队名称,但我不断收到上述错误。
非常感谢任何帮助。
答案 0 :(得分:1)
要简单地显示User对象中的所有团队名称,请使用以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userTeamRef = rootRef.child("User").child(uid).child("team");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String teamName = ds.getValue(String.class);
Log.d("TAG", teamName);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
userTeamRef.addListenerForSingleValueEvent(valueEventListener);
输出将是:
AHL1
AHL2
答案 1 :(得分:0)
Map singleTeam = (Map) entry.getValue();
你Map
是Map<String,Object>
所以当数据来自键值对时,值的类型为String。因此,entry.getValue()
将返回String
类型的值,并将其转换为Map
,因此会出错。
String singleteam = entry.getValue();
答案 2 :(得分:0)
很明显,因为您的Map singleTeam = (Map) entry.getValue();
调用正在返回一个String
对象,而不是Map
,这是您尝试将其强制转换的对象,因此会出现转换异常错误。
虽然我看到您的地图为Map<String, Object>
如果您希望通过entry.getValue();
返回地图,请在投射前检查它,如下例所示:
for (Map.Entry<String, Object> entry : team.entrySet())
{
if( entry instanceof Map )
{
Map singleTeam = (Map) entry.getValue();
}
else
{
String singleTeam = (String) entry.getValue()
}
}