0

Sorry for this beginner question, but being very new to java, I can't figure out how to get value from a method in another file.

Here is my code fragment: First, I have defined a string and used it inside a class:

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.menu_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(date);
                    showDateDialog();   //**I want this showDateDialogue to update mTextMessage**//
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.menu_location);
                    return true;
            }
            return false;
        }
    };



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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    public void showDateDialog() {
    DialogFragment CalFragment = new CalFragment();
    CalFragment.show(getSupportFragmentManager(), "timePicker");
    date = "Hello";
    }
}

where CallFragement is in another class:

public class CalFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
        Date d = new Date(year, month, day);
        String strDate = dateFormatter.format(d);
    }
}

So, Basically, I want to use the value of this strDate in the mTextMessage! But, I am very very new to java and though this type of question is very common in SO, I can't figure out a solution.

Kindly Help.

UPDATE Hi, though I understand its a trivial question, but I have managed to move only a little in last 5 hour. I humbly request you to help a bit. Here is my updated code:

MainCativity.java

public class MainActivity extends AppCompatActivity
    implements CalFragment.OnDateSelected{

    private TextView mTextMessage;
    String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.menu_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(date);
                    showDateDialog();
                    mTextMessage.setText(date);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.menu_location);
                    return true;
            }
            return false;
        }
    };



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

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    public void showDateDialog() {
    DialogFragment CalFragment = new CalFragment();
    CalFragment.show(getSupportFragmentManager(), "timePicker");
    date = "Hello";
    }
}

and

CalFragment.java

public class CalFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {
    OnDateSelected mcallback;
    public interface OnDateSelected {
        public void NewStrDate(String strDate);
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
        Date d = new Date(year, month, day);
        String strDate = dateFormatter.format(d);
        mcallback.NewStrDate(strDate);
    }
}

and this is giving error:

Error:(16, 8) error: MainActivity is not abstract and does not override abstract method NewStrDate(String) in OnDateSelected
BaRud
  • 3,055
  • 7
  • 41
  • 89

0 Answers0