Skip to content Skip to sidebar Skip to footer

How To Call Non-static Method Of Another Class

I want to understand how to call a non-static method of one class in another class. I have seen something like the following: Class1 c1 = new Class1(); c1.doSomething(); when the

Solution 1:

You don't need class inheritance at all to acheive the problem in your question.

I was able to throw this working example together that resets both pickers to the current date.

MainActivity

publicclassMainActivityextendsActivity {

    private Button resetButton;
    private DatePicker datePicker;
    private TimePicker timePicker;

    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resetButton = (Button) findViewById(R.id.btn_reset);
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            publicvoidonClick(View view) {
                Calendar c = Calendar.getInstance();

                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);

                datePicker.init(year, month, day, null);

                int hour = c.get(Calendar.HOUR_OF_DAY);
                int minute = c.get(Calendar.MINUTE);

                timePicker.setCurrentHour(hour);
                timePicker.setCurrentMinute(minute);
            }
        });

    }
}

activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Reset"android:id="@+id/btn_reset"/><DatePickerandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/datePicker"android:layout_gravity="center_horizontal"/><TimePickerandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:id="@+id/timePicker"/></LinearLayout>

Or if you want methods to set the date and time, you can do this

resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            publicvoidonClick(View view) {
                Calendar c = Calendar.getInstance();
                setDate(c);
                setTime(c);
            }
        });


} // end onCreateprivatevoidsetDate(Calendar c) {
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    datePicker.init(year, month, day, null);
}

privatevoidsetTime(Calendar c) {
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    timePicker.setCurrentHour(hour);
    timePicker.setCurrentMinute(minute);
}

Post a Comment for "How To Call Non-static Method Of Another Class"