E
E
etozhesano2020-07-21 19:16:20
Java
etozhesano, 2020-07-21 19:16:20

How to extract a variable from a method for use in another class?

Good afternoon. I'm new to Java and Android, I can't seem to get information out of a method and move it to another class. Briefly: there is a custom Calendar connected to the project as a library, there is a separate module that has the MainActivity class, it has a method:

@Override
    public Date onDayClick(Date date) {
        Toast.makeText(this, "onDayClick: " + date, Toast.LENGTH_SHORT).show();

        return date;
    }


A method that displays a window on which date was clicked on the calendar. When Ctrl+clicking on the method, it refers to a custom calendar from library.

How can I implement this method to store the date in the MainActivity class and then pass it to the EditText field in another class? With the transfer, as I understand it, I need PutExtra, but I cannot save this data in the MainActivity class. If you save in the method, then you can see the data through sout, but it doesn’t work outside the method. I can’t understand why (

I tried it through getters, but it requires static, it doesn’t suit me. Maybe, of course, the problem is banal, but my theory is weak, so I’m stupid out of the blue

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekseyHunter, 2020-07-22
@etozhesano

Another class is Activity/Fragment? Yuzai

Intent intent = new Intent('класс');
Bundle bundle = new Bundle();
bundle.putString("date",calendar.getDate().toString());
intent.putExtra(bundle);
startActivity(intent);

In the OnCreate class of the new Activity / Fragment, you pull out the saved date from the Bundle, look for the EditText through findViewById, and add it to the text field.
If the other class is the one you created, you pass the date from the calendar to the constructor, and then you access the field of this object and write it to the EditText.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question