我遇到以下错误
com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.mscit_lab_16.school_management.Message
类型我遇到了问题
m.setMsg(ds.getValue(Message.class).getMsg());
帮我解决这个错误。
有一个Message类
package com.mscit_lab_16.school_management;
import android.support.annotation.Keep;
import com.google.firebase.database.Exclude;
/**
* Created by PC PLANET on 12/05/2018.
*/
//@JsonIgnoreProperties(ignoreUnknown = true)
@Keep
public class Message {
public String msg;
public Message() {
}
@Keep
public String getMsg() {
return msg;
}
@Keep
public void setMsg(String msg) {
this.msg = msg;
}
}
Cont_admin.java
package com.mscit_lab_16.school_management;
import android.app.Dialog;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.firebase.client.Firebase;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.ChildEventListener;
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.internal.FirebaseAppHelper;
import android.database.Cursor;
import java.util.ArrayList;
public class Cont_Admin extends AppCompatActivity {
EditText msgText;
Button sendBtn;
ArrayList<Message> msgs=new ArrayList<Message>();
RecyclerView rv;
MyAdapter adapter;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cont__admin);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
rv=findViewById(R.id.mRecyclerID);
rv.setLayoutManager(new LinearLayoutManager(this));
databaseReference= FirebaseDatabase.getInstance().getReference("admin_messages");
Firebase.setAndroidContext(this);
//REFERENCE
this.refreshData();
}
public void sendMesssage_admin(View view) {
displayDialog();
}
private void displayDialog(){
Dialog d=new Dialog(this);
d.setTitle("Send Message");
d.setContentView(R.layout.dialoglayout);
msgText=d.findViewById(R.id.nameEditText);
sendBtn=d.findViewById(R.id.sendBtn);
d.show();
}
public void sendMsg(View view) {
sendMessage(msgText.getText().toString().trim());
msgText.setText("");
refreshData();
}
//send msg to firebase database
private void sendMessage(String message){
Message m=new Message();
m.setMsg(message);
databaseReference.push().setValue(m);
}
//retrieve msg from firebase database
private void refreshData(){
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getUpdates(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
getUpdates(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getUpdates(DataSnapshot dataSnapshot){
msgs.clear();
for (DataSnapshot ds:dataSnapshot.getChildren()){
Message m=new Message();
m.setMsg(ds.getValue(Message.class).getMsg());
msgs.add(m);
}
if (msgs.size()>0){
adapter=new MyAdapter(Cont_Admin.this,msgs);
rv.setAdapter(adapter);
}else {
Toast.makeText(Cont_Admin.this,"No data",Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
for (DataSnapshot ds:dataSnapshot.getChildren()){
Message m=new Message();
m.setMsg(ds.getValue(Message.class).getMsg());
msgs.add(m);
}
这部分是有问题的dataSnapshot已经是'admin_messages'的子节点,因此它是由push()
方法推送的节点。 dataSnapshot只有child,它是一个键为msg
的字符串,因此;循环子女是不必要的
所以,删除并保持它像:
Message m = dataSnapshot.getValue(Message.class);
msgs.add(m);