Firebase query result in ListView UI and RecyclerView UI

不打扰是莪最后的温柔 提交于 2019-12-12 03:48:11

问题


I am trying to create a Firebase database to store employee information and then when an address is queried, all the employees staying in that particular area should be displayed in a ListView or a RecyclerView however, I am having the following problem:

I am unable to populate the ListView.

    ArrayList<String> mItems = new ArrayList<>();
    ArrayAdapter mAdapter;
    ListView mRecyclerView;

    FirebaseListAdapter<String> fireAdapter = new FirebaseListAdapter<String>() {
            @Override
            protected void populateView(View view, String s) {
             //populate view
            }
        };

In the above code, I am unable to write the constructor so that it becomes,

    FirebaseListAdapter<String> fireAdapter = new FirebaseListAdapter<String>(this,String.class,
                android.R.layout.simple_list_item_1,mEmployRef) {
            @Override
            protected void populateView(View view, String s) {
                //populate view
            }
        }; 

When I write the above code with the constructor, it says,

Cannot resolve constructor 'FirebaseListAdapter(anonymous android.view.View.OnClickListener, java.lang.Class, int, com.google.firebase.database.DatabaseReference)'

How do I resolve this? A similar problem occurs when I try to use a RecyclerView.


回答1:


You're creating the adapter inside an OnClickListener(), which means that this points to that listener. You need to pass in the Activity to FirebaseListAdapter, which you can get with MainActivity.this.




回答2:


I was having this problem when Firebase upgraded and what i did to get out of the constructor error was change this:

Firebase rootRef = new Firebase("https://<your-app>.firebaseio.com/");

to this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

then use the rootRef as the fourth parameter on the constructor.

here is a link for upgrading from the previous firebase to the current: https://firebase.google.com/support/guides/firebase-android#import_your_project_to_the_new_firebase_console_numbered

Also, upgrade your sdk. Hope this solves your problem



来源:https://stackoverflow.com/questions/37450073/firebase-query-result-in-listview-ui-and-recyclerview-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!