从Firebase数据库检索数据并填充Listview

时间:2016-09-15 21:20:19

标签: android firebase firebase-realtime-database android-arrayadapter

我正在尝试使用数组适配器从Firebase数据库中检索数据以填充列表视图但没有成功?怎么办呢?

这是我的ArrayAdapter类。

public class ModelListAdapter extends ArrayAdapter<Model> {
    ArrayList<Model> mode = new ArrayList<Model>();
    private Query mRef;
    private Class<Model> mModelClass;
    private int mLayout;
    private Context context;
    private List<Model> mModels;
    private Activity activity;


    public ModelListAdapter(Activity activity, int mLayout ,Class<Model> mModelClass, Query mRef) {
        super(activity,mLayout,mModelClass,mRef);
        this.mRef = mRef;
        this.mModelClass = mModelClass;
        this.mLayout = mLayout;
        mInflater = activity.getLayoutInflater();
    }

这是MainActivity类。

public class MainActivity extends AppCompatActivity {

    private static final String FIREBASE_URL = "https://XYZ.firebaseio.com/";
    private ModelListAdapter adapter;
    private ListView listView;
    private FirebaseApp app;
    private Firebase ref;
    private FirebaseDatabase database;
    private FirebaseStorage storage;

    private DatabaseReference databaseRef;
    private StorageReference storageRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.logo);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient));
        setContentView(R.layout.activity_main);

        Firebase.setAndroidContext(this);

        app = FirebaseApp.getInstance();
        database = FirebaseDatabase.getInstance(app);
        storage = FirebaseStorage.getInstance(app);

        databaseRef = database.getReference(FIREBASE_URL);
        ref = new Firebase(FIREBASE_URL);

        adapter = new ModelListAdapter(this,Model.class,R.layout.list_item,ref);
        listView.setAdapter(adapter);
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
            }
        });

        databaseRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Model model = dataSnapshot.getValue(Model.class);
                adapter.mode.add(model);

            }

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

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

我收到错误

  

找不到适合ArrayAdapter的构造函数(Activity,int,class&lt;&gt;,Query)
  (参数不匹配;类&lt;&gt;不能为int)

0 个答案:

没有答案