从另一个类的方法中获取值

时间:2018-02-09 15:11:53

标签: java android methods

对于这个初学者的问题很抱歉,但对java来说还是一个新手,我无法弄清楚如何从另一个文件中的方法获取值。

这是我的代码片段: 首先,我已经定义了一个字符串并在类中使用它:

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";
    }
}

其中CallFragement在另一个类中:

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

所以,基本上,我想在strDate中使用此mTextMessage的值!但是,我对java非常新,虽然这类问题在SO中很常见,但我无法找到解决方案。

请帮助。

更新 嗨,虽然我理解它是一个微不足道的问题,但我在过去的5个小时内只成功了一点。我谦卑地请你帮忙。这是我更新的代码:

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";
    }
}

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

这是错误的:

Error:(16, 8) error: MainActivity is not abstract and does not override abstract method NewStrDate(String) in OnDateSelected

0 个答案:

没有答案