0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nikhil Raghavendra
  • 1,570
  • 5
  • 18
  • 25

2 Answers2

0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Still gives me the same error sir, FirebaseListAdapter fireAdapter = new FirebaseListAdapter(Search.this,String.class,...). Doing the above still gives me the same error, pls help. It is true that this is inside the OnClickListener() but passing Search.this gives me the same error. – Nikhil Raghavendra May 26 '16 at 02:10
  • @puf, switched over to listView and it works, but not realtime though. But its ok for this app. – Nikhil Raghavendra May 26 '16 at 06:48
0

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

Higen
  • 77
  • 5