com.google.firebase.database.DatabaseException:无法在显示列表视图中将类型java.util.HashMap的值转换为String

时间:2019-03-01 02:35:02

标签: android

已编辑

我遇到类似“ com.google.firebase.database.DatabaseException的错误:在显示列表视图中无法将类型java.util.HashMap的值转换为String。”

这是我的课程。有人可以帮我吗?谢谢大家!

public class ViewManager extends AppCompatActivity {

    private  ListView listview;
    private FirebaseDatabase firebaseDatabase;
    private DatabaseReference ref;
    private ArrayList<String> Managername=new ArrayList<>();
//  String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     // managerAddonDB=new ManagerAddonDB();
        setContentView(R.layout.activity_view_manager);

        listview=(ListView)findViewById(R.id.managerlistview);
        firebaseDatabase=FirebaseDatabase.getInstance();
        ref=firebaseDatabase.getReference("Manager");

        final  ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>
            (this,android.R.layout.simple_list_item_1,Managername);
        listview.setAdapter(arrayAdapter);
        ref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                String value=dataSnapshot.getValue(String.class);
                Managername.add(value);
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
    });

1 个答案:

答案 0 :(得分:0)

Firebase数据库以值对(Hashmap)形式返回数据快照值,因此,如果要以String形式获取该值,请使用

String value=dataSnapshot.getValue().toString()

代替

String value=dataSnapshot.getValue(String.class);

还添加一个条件,即数据快照不为null。 还要检查dataSnapshot.getValue()和dataSnapshot.getValue(Java.class)函数here.