Firebase检索数据错误

时间:2017-02-22 11:00:25

标签: android firebase android-database

我正在尝试从firebase检索数据到listview ...但是有一行代码会崩溃应用程序, mDatabase.addValueEventListener((com.google.firebase.database.ValueEventListener)侦听器);

崩溃报告 android addvalueeventlistener无法强制转换为com.google.firebase.database.ValueEventListener

我不知道如何解决这个问题:

package com.example.moses.mcn;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Map;

public class Business extends AppCompatActivity {
    private ListView business;

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

    public ArrayList<String> arr;
    public ArrayAdapter adapter;

    @Override
    protected void onStart() {
        super.onStart();

        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
        arr = new ArrayList<>();

        ValueEventListener listener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                map2list((Map) dataSnapshot.getValue());
                //formats the datasnapshot entries to strings
                adapter.notifyDataSetChanged();
                //makes the ListView realtime
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }


            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                System.out.println(databaseError.toException());
                // ...
            }
        };
        mDatabase.addValueEventListener((com.google.firebase.database.ValueEventListener) listener);

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, arr);

        ListView listView = (ListView) findViewById(R.id.business);
        listView.setAdapter(adapter);


    }

    public void map2list(Map<String,Long> map){

        arr.clear();
        for (Map.Entry<String, Long> entry : map.entrySet()) {

            Long key = Long.parseLong(entry.getKey());
            String d = DateFormat.getDateTimeInstance().format(key);
            Long value = entry.getValue();
            arr.add(d + ": " + value);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

将导入更改为

 import com.google.firebase.database.ValueEventListener

import com.firebase.client.ValueEventListener