0

My previous question get the value from a method in another class is understandably closed because its duplicate, but I still can't find a way to solve the problem. I will be grateful if someone kindly help. I have progressed a little, but thats not enough. First, my current code:

MainActivity.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);
    }
}

I want to supply the date from CalFragment to ShowDate dialogue, but its not working. At the present state, it shows implements declaration in MainActivity is faulty. I am reading some java tutorial, but its just my 2nd day. So kindly help a bit.

TM00
  • 1,330
  • 1
  • 14
  • 26
BaRud
  • 3,055
  • 7
  • 41
  • 89
  • Because `CalFragment.OnDateSelected` interface declares a method `NewStrDate`, you must implement it in `MainActivity` class. Have you tried that? – curlyBraces Feb 10 '18 at 06:34
  • No. Can you please show me what to do? – BaRud Feb 10 '18 at 06:44
  • If you add a method like ` @Override public void NewStrDate(String strDate) { }` in `MainActivity`, the error with `implements` declaration should go. But I would suggest, if you are just starting out with Java, you should practice on some simpler tutorial. – curlyBraces Feb 10 '18 at 06:47
  • yes...it goes, but it still cant get the date chosen. Kindly help – BaRud Feb 10 '18 at 06:50

0 Answers0