我的错误:
E/UncaughtException: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.stitch.fractions.Profile.userInfo.UserInformation
at com.google.android.gms.internal.zzamy.zze(Unknown Source)
at com.google.android.gms.internal.zzamy.zzb(Unknown Source)
at com.google.android.gms.internal.zzamy.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.stitch.fractions.Scoreboard.activity.activity.ScoreboardActivity$1.onDataChange(ScoreboardActivity.java:49)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzajp.zza(Unknown Source)
at com.google.android.gms.internal.zzakp.zzcxi(Unknown Source)
at com.google.android.gms.internal.zzaks$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
I/FA: Tag Manager is not found and thus will not be used
I/Process: Sending signal. PID: 22183 SIG: 9
Disconnected from the target VM, address: 'localhost:8604', transport: 'socket'
我的活动类:
public class ScoreboardActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private DatabaseReference dbRef;
private ArrayList<UserInformation> mArrayUserInfo = new ArrayList<>();
private String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scoreboard);
mAuth = FirebaseAuth.getInstance();
userId = mAuth.getCurrentUser().getUid();
dbRef = FirebaseDatabase.getInstance().getReference();
displayData();
}
public void displayData() {
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
for (DataSnapshot user : users.getChildren()) {
if (user.getKey().equals(userId)) {
for (DataSnapshot sItem : user.getChildren()) {
if (!(sItem.getKey().equals("age"))) {
UserInformation scoreActivity = sItem.getValue(UserInformation.class);
mArrayUserInfo.add(scoreActivity);
ScoreAdapter sAdapter = new ScoreAdapter(getApplicationContext(), mArrayUserInfo);
ListView listScore = (ListView)findViewById(R.id.lvScore);
listScore.setAdapter(sAdapter);
}
}
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ScoreboardActivity.this, "Error Kid", Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的模型类:
public class UserInformation {
public String name;
public String age;
public String score;
public UserInformation() {
}
public UserInformation(String name, String age, String score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public String getScore(){
return score;
}
}
}
以下是我写给数据库的方式:
public class ProfileActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private TextView mUserEmail, mUserScore;
private Button mBtnLogOut, mBtnSave, mBtnScore;
private EditText mEdtProfileName, mEdtProfileAge;
private DatabaseReference dbRefer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
initComponent();
getCurrentUser();
checkCurrentUser();
firebaseDatabase();
mBtnLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firebaseAuth.signOut();
finish();
Intent back = new Intent(ProfileActivity.this, HomeActivity.class);
startActivity(back);
Toast.makeText(ProfileActivity.this, "Good Bye", Toast.LENGTH_SHORT).show();
}
});
mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveUserInfor();
}
});
mBtnScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent game = new Intent(ProfileActivity.this, ScoreboardActivity.class);
startActivity(game);
}
});
}
public void initComponent() {
mBtnLogOut = (Button) findViewById(R.id.btnOut);
mUserEmail = (TextView) findViewById(R.id.userEmail);
firebaseAuth = FirebaseAuth.getInstance();
mEdtProfileName = (EditText) findViewById(R.id.edtProfileName);
mEdtProfileAge = (EditText) findViewById(R.id.edtProfileAge);
mBtnSave = (Button) findViewById(R.id.btnSave);
mUserScore = (TextView) findViewById(R.id.userScore);
mBtnScore = (Button) findViewById(R.id.btnScore);
}
public void checkCurrentUser() {
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() == null) {
finish();
Intent home = new Intent(ProfileActivity.this, HomeActivity.class);
startActivity(home);
}
}
public void getCurrentUser() {
FirebaseUser user = firebaseAuth.getCurrentUser();
mUserEmail.setText("Current User: " + user.getEmail());
}
public void firebaseDatabase() {
dbRefer = FirebaseDatabase.getInstance().getReference();
mEdtProfileName.getText().toString().trim();
mEdtProfileAge.getText().toString().trim();
}
public void saveUserInfor() {
String name = mEdtProfileName.getText().toString().trim();
String age = mEdtProfileAge.getText().toString().trim();
SharedPreferences prefsName = getSharedPreferences("Name", MODE_PRIVATE);
SharedPreferences.Editor editorName = prefsName.edit();
editorName.putString("sentname", name);
editorName.commit();
SharedPreferences prefsAge = getSharedPreferences("Age", MODE_PRIVATE);
SharedPreferences.Editor editorAge = prefsAge.edit();
editorAge.putString("sentage", age);
editorAge.commit();
SharedPreferences prefs = getSharedPreferences("Data",MODE_PRIVATE);
String data = prefs.getString("time","hi");
if (TextUtils.isEmpty(name)) {
mEdtProfileName.setError("Empty Field !!!");
return;
}
if (TextUtils.isEmpty(age)) {
mEdtProfileAge.setError("Empty Field !!!");
return;
}
UserInformation userInfo = new UserInformation(name, age, data);
FirebaseUser user = firebaseAuth.getCurrentUser();
dbRefer.child("User").child(user.getUid()).setValue(userInfo);
Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
}
}
我的适配器类:
public class ScoreAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mScoreInflater;
private ArrayList<UserInformation> mUserInfo;
public ScoreAdapter(Context mContext, ArrayList<UserInformation> userInfo) {
this.mContext = mContext;
this.mScoreInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mUserInfo = userInfo;
}
@Override
public int getCount() {
return mUserInfo.size();
}
@Override
public Object getItem(int position) {
return mUserInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
classScoreHolder holder;
if (convertView == null) {
convertView = mScoreInflater.inflate(R.layout.scoreboard_row, parent, false);
holder = new classScoreHolder();
holder.mTvScoreName = (TextView) convertView.findViewById(R.id.tvScoreName);
holder.mTvScorePoint = (TextView) convertView.findViewById(R.id.tvScorePoint);
convertView.setTag(holder);
} else {
holder = (classScoreHolder) convertView.getTag();
}
TextView tvScoreName = holder.mTvScoreName;
TextView tvScorePoint = holder.mTvScorePoint;
UserInformation getUserInfo = (UserInformation) getItem(position);
tvScoreName.setText("User Name: " + getUserInfo.getName());
tvScorePoint.setText("User Score: " + getUserInfo.getScore());
return convertView;
}
private static class classScoreHolder {
public TextView mTvScoreName;
public TextView mTvScorePoint;
}
当前数据库有名称,年龄和分数。我想要获得的只是从Firebase数据库到Customlistview的名称和年龄。
答案 0 :(得分:0)
不是100%肯定,但看起来你的层次结构太过分了。如果我是对的,这将简化您的代码。试试这个:
public void displayData() {
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot users : dataSnapshot.getChildren()) {
for (DataSnapshot user : users.getChildren()) {
if (user.getKey().equals(userId)) {
UserInformation scoreActivity = user.getValue(UserInformation.class);
mArrayUserInfo.add(scoreActivity);
}
}
}
ScoreAdapter sAdapter = new ScoreAdapter(getApplicationContext(), mArrayUserInfo);
ListView listScore = (ListView)findViewById(R.id.lvScore);
listScore.setAdapter(sAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ScoreboardActivity.this, "Error Kid", Toast.LENGTH_SHORT).show();
}
});
}