Suresh
Suresh

Reputation: 13

Unable to use Context inside Firebase valueEventListener

I am trying to retrieve a list of items from Firebase and after all the data is retrieved, I want to inflate the data in listView using an adaptor. Here is the code I am using:

vehicleReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){

                    for (DataSnapshot data : dataSnapshot.getChildren()){
                        ServiceHistoryDataModel serviceHistoryData = data.getValue(ServiceHistoryDataModel.class);
                        serviceHistoryDataList.add(serviceHistoryData);
                    }

                    ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter (this,serviceHistoryDataList,getLayoutInflater());
                    listView.setAdapter(adapter);

                }else
                    Toast.makeText(ServiceHistoryActivity.this, "No service history available !", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

(please refer the screenshot)

I am getting an error :-

"ServiceHistoryListAdpter() in ServiceHistoryListAdapter cannot be applied:..."

For the line:

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter (this,serviceHistoryDataList,getLayoutInflater());

Could you please let me know how can I solve this?

enter image description here

Upvotes: 0

Views: 389

Answers (5)

Abdul Wadood
Abdul Wadood

Reputation: 1171

You want to access the instance of enclosing class from the anonymous class. The syntax for which is EnclosingClassName.this. So change the line to:

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter(YourActivityName.this,serviceHistoryDataList,getLayoutInflater());

i.e. add YourActivityName. before this.

Upvotes: 4

user5876477
user5876477

Reputation:

Replace your code with this.

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter(ActivityName.this,serviceHistoryDataList,getLayoutInflater());

Upvotes: 1

Prakhar Gupta
Prakhar Gupta

Reputation: 131

Please replace

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter(this,serviceHistoryDataList,getLayoutInflater());

with

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter(YourActivityName.this,serviceHistoryDataList,getLayoutInflater());

Upvotes: 1

Dishant Kawatra
Dishant Kawatra

Reputation: 648

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter(YourActivityName.this,serviceHistoryDataList,getLayoutInflater());

Upvotes: 0

Rishabh Saxena
Rishabh Saxena

Reputation: 1795

Just pass parameter like "YourActivityName.this" your problem will be resolved. Check out code below for the same.

ServiceHistoryListAdapter adapter = new ServiceHistoryListAdapter(YourActivityName.this,serviceHistoryDataList,getLayoutInflater());

Upvotes: 1

Related Questions