当前位置: 首页 > 知识库问答 >
问题:

我正试图从firebase获取多个嵌套数据,但得到这个hashmap错误

慕乐语
2023-03-14
   ListView listView;
   FirebaseDatabase firebaseDatabase;
   DatabaseReference databaseReference;
   ArrayList<String> arrayList = new ArrayList<>();
   ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Users");


    listView = (ListView) findViewById(R.id.listview);

       Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
        databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.i( "he","Reference : " + databaseReference.toString() );
            String value = dataSnapshot.getValue(String.class);
            arrayList.add(value);
            arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
            listView.setAdapter(arrayAdapter);

        }

这是我得到的错误:

共有1个答案

龙枫
2023-03-14

在这一行中

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

你正在试图提取价值。试图在=的左侧设置字符串值,所以在=的右侧也设置为字符串。要在getValue中实现这一点,您需要使用您的类POJO。并且只有在提取值之后。例如:

User user = dataSnapshot.getValue(User.class);
String finalValue = user.getSomeString();
 类似资料: