How to initialize interface in Activity.

时间:2017-08-04 12:08:09

标签: java android android-fragments android-activity interface

I need to call a method in fragment when a certain item is selected in navigation drawer activity. For this, I've created one interface which I Will be initializing & calling a method from the activity, Also I'll implement this interface in Fragment and override this method. Here is code snippet for declaring an interface.

public interface AlertForDiscardDefaultProfileChanges {
     void alertForDiscardDefaultProfileChanges(int navigationItemID);
}

And this is how I'm initializing in activity.

    private AlertForDiscardDefaultProfileChanges alertForDiscardDefaultProfileChanges;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        alertForDiscardDefaultProfileChanges = (AlertForDiscardDefaultProfileChanges) this;
    }

Here I'm getting java.lang.ClassCastException for initializing it. Not sure what I'm missing here or what's wrong.

3 个答案:

答案 0 :(得分:0)

you have to implements this interface to your activity/fragment for eg :

MainActivity :

    public class MainActivity extends AppCompatActivity implements FragmentClassName.AlertForDiscardDefaultProfileChanges{

//override methods

@Override
    public void alertForDiscardDefaultProfileChanges(String navigationItemID) {
// now use navigationItemID here...
    }
}

答案 1 :(得分:0)

Step-1

public interface AlertForDiscardDefaultProfileChanges {

     void alertForDiscardDefaultProfileChanges(int navigationItemID);
}

define this method from which activity and class you want transfer your data

Step 2 -

Define this method also in step 1 class.

private  AlertForDiscardDefaultProfileChanges favListner;

public void setAlertOnDiscardListner(AlertForDiscardDefaultProfileChanges 
                                favOnTouchListner) {

        favListner = favOnTouchListner;
    }

Step -3 .

pass value from Step 1 class like below

favListner.alertForDiscardDefaultProfileChanges(int navigationItemID);

Step-4 In which class you want data first implement that interface like.

Class A implements YourActivity_where_interface define.AlertForDiscardDefaultProfileChanges{

override Method.
}

Step 5.

You Should have to do one thing also in that class where you want data .

You should have to initialise interface from on Create method like below.

YourActivity_where_interface define.setAlertOnDiscardListner(this);

Done now you can play with it.

答案 2 :(得分:0)

以下是我如何实现它, 我通过片段的对象调用了一个方法,我希望在其中实现一个方法。

片段类 -

 public class DefaultProfileFragment extends Fragment implements 
 AlertForDiscardDefaultProfileChanges {
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_default_profile, container, 
    false);
    mContext = view.getContext();

    return view;
}

@Override
public void alertForDiscardDefaultProfileChanges(int navigationItemID) {
    showDismissWarning(navigationItemID);
}

我需要调用interface方法的Activity。 片段类的简单对象和方法的名称。没有什么花哨的东西需要做。无需初始化界面。

(new DefaultProfileFragment()).alertForDiscardDefaultProfileChang‌​‌​es(id);