Vinayak Khedkar
Vinayak Khedkar

Reputation: 494

How to access object in interface method implemented inline in getInstance method of same object

I am creating Dialog Fragment Object in Adapter where getInstance() method of that dialog accept interface object which provide method to delete object. in this case i want to dismiss the dialog once object deleted. but it shows error that the dailog object might not be initialize.

For example I am providing part of getVew() method of adapter:

final MyDailog dailog = MyDailog.getInstance((int ObjectValue, position, new MyDailog.OnDelete() {
        @Override
        public void onDeleteItem() {

                 objectList.get(position).setDeleted();
                 dailog.dismiss();                   
        });

here position is index of view in getView Method of adapter

@ line "dailog.dismiss();" it gives me error that Error:(182, 45) error: variable dailog might not have been initialized

Upvotes: 0

Views: 87

Answers (1)

JP Moresmau
JP Moresmau

Reputation: 7403

Indeed the compiler is right: it cannot guarantee that getInstance does not call onDeleteItem before returning, and in this case dailog would not be initialized. The interface OnDelete is yours, isn't it? Modify it so that onDeleteItem has the dialog as a parameter.

final MyDailog dailog = MyDailog.getInstance((int ObjectValue, position, new MyDailog.OnDelete() {
    @Override
    public void onDeleteItem(MyDailog md) {

             objectList.get(position).setDeleted();
             md.dismiss();                   
    });

And of course modify the code calling onDeleteItem to pass the create MyDailog instance.

Upvotes: 1

Related Questions