the_prole
the_prole

Reputation: 8985

Cannot pass a custom adapter through a constructor

I have three fragment classes each with it's own custom adapter

//Fragment class 1
static CusomtAdapter1 adapter1;

//Fragment class 2
static CusomtAdapter2 adapter2;

//Fagment class 3
static CusomtAdapter3 adapter3;

All three adapters are based on the same underlying data. I want to call notifyDataSetChanged() on all of them at the same time. So I pass them to another class.

//Fragment class 1
UpdateAdapters updateAdapters = new UpdateAdapters(adapter1);

//Fragment class 2
UpdateAdapters updateAdapters = new UpdateAdapters(adapter2);

//Fagment class 3
UpdateAdapters updateAdapters = new UpdateAdapters(adapter3);

This is is UpdateAdapters class

public class UpdateAdapters {

    private static CusomtAdapter1 adapter1;
    private static CusomtAdapter2 adapter2;
    private static CusomtAdapter3 adapter3;

    //Public Constructors
    public void UpdateAdapters(CusomtAdapter1 adapter1){
        this.adapter1 = adapter1;
    }

    public void UpdateAdapters(CusomtAdapter2 adapter2){
        this.adapter2 = adapter2;
    }

    public void UpdateAdapters(CusomtAdapter3 adapter3){
        this.adapter3 = adapter3;
    }

    //Public methods
    public static void update(){
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
        adapter3.notifyDataSetChanged();
    }
}

And I can update them all like so

UpdateAdapters.update()

My problem is I cannot pass my adpaters to my update class

//Fragment class 1
UpdateAdapters updateAdapters = new UpdateAdapters(adapter1); // error: UpdateAdapters() cannot be applied to come.example.myname.myapp.CusomtAdapter1

//Fragment class 2
UpdateAdapters updateAdapters = new UpdateAdapters(adapter2); // error: UpdateAdapters() cannot be applied to come.example.myname.myapp.CusomtAdapter2

//Fagment class 3
UpdateAdapters updateAdapters = new UpdateAdapters(adapter3); // error: UpdateAdapters() cannot be applied to come.example.myname.myapp.CusomtAdapter3

Whats going on?

Thanks

Upvotes: 0

Views: 92

Answers (1)

Blo
Blo

Reputation: 11988

As @MikeM. pointed out, your "constructors" are basically methods. Because they are not returning an instance of objects but they return void. So, to get a constructor, you have to do:

public UpdateAdapters(...) { }

However, you should create one single instance for all fragments, instead of create a new one each time you set an adapter. In order to do that, you can create an instance with the following:

public class UpdateAdapters {
    private static UpdateAdapters instance = null;

    // constructor
    protected UpdateAdapters() { }

    // get the current instance
    public static UpdateAdapters getInstance() {
        if (instance == null) {
            // create a new one if it doesn't exist
            instance = new UpdateAdapters();
        }
        return instance;
    }
    ...
}

In the fragments, when you want to add the adapters, you'll get the current instance of this object (and don't create everytime a new one) and set the adapters into it. So, you will call:

UpdateAdapters.getInstance().doSomething();

to get the current object created and to do something with this object's instance.
You could thus set the fragments' adapters. Assume you have this in UpdateAdapters class:

public class UpdateAdapters {
    ...
    private CusomtAdapter1 adapter1;
    private CusomtAdapter2 adapter2;
    private CusomtAdapter3 adapter3;

    public void setAdapterOne(CusomtAdapter1 adapter1) {
        this.adapter1 = adapter1;
    }

    public void setAdapterTwo(CusomtAdapter2 adapter2) {
        this.adapter2 = adapter2;
    }

    public void setAdapterThree(CusomtAdapter3 adapter3) {
        this.adapter3 = adapter3;
    }

    public void update() {
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
        adapter3.notifyDataSetChanged();
    }
}

Then, in each fragment, you will add its adapter by using:

UpdateAdapters.getInstance().setAdapterOne(adapter1); // same for each one

And finally, update them, with the same stuff:

UpdateAdapters.getInstance().update();

Upvotes: 2

Related Questions