I have two classes. One is my main activity and the other one a helper method which is supposed to open an Android DatePicker. Everything worked fine so far, except if I want to retrieve the date from the date picker class. Here are my classes:
// This is the class that creates the date picker
package com.strom.myapp;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;
import java.util.Calendar;
public class DefaultDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private String returnDate;
public String getReturnDate() {
return returnDate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
returnDate = String.valueOf(dayOfMonth) +
"." +
String.valueOf(monthOfYear) +
"." +
String.valueOf(year) +
", 00:00";
}
}
And here is the main activity
package com.strom.myapp;
import android.app.DialogFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BalanceScreen extends ActionBarActivity {
private String payDate;
// ... omitted code ...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_payDate) {
DialogFragment datePicker = new DefaultDatePicker();
datePicker.show(getFragmentManager(), "datePicker");
payDate = datePicker.getReturnDate();
return true;
}
// ... omitted code ...
}
The getter-method 'datePicker.getReturnDate();' is not accessible from the main activity, even though they are in the same package. I don't see why this shouldn't work. What am I missing here?
i will suggest you to replace:
DialogFragment datePicker = new DefaultDatePicker();
with
DefaultDatePicker datePicker = new DefaultDatePicker();
Because your method getReturnDate is declared in DefaultDatePicker, and not in DialogFragment.
您可以将datePicker
(它提供DefaultDatePicker的实例)强制转换为DefaultDatePicker
以调用特定于DatePickerDialog.OnDateSetListener
。
((DefaultDatePicker)datePicker).getReturnDate()
Add a public variable to your activity say
public String dateFromPicker = "";
In DefaultDatePicker
method onDateSet
do this
((BalanceScreen)getActivity()).dateFromPicker = returnDate;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.