1

이상한 문제를 해결하려고합니다. 나는 안드로이드 프로그래밍 (그리고 포스터로서의이 사이트)에 초보자 다. 그래서 나와 조금만 견뎌 라. 나는 내 주 활동에서 호출 한 datePickerDialog를 가지고 있으며 그 클래스 내의 TextView는 년, 월, 일을 올바르게 업데이트하지만, 주된 활동에서 일, 월, 일을 알아 내기 위해 게터를 사용할 때 어떤 이유로, 그냥 0을 반환합니다. getters를 사용하여 내가 잘못한 것을하고 있습니까?datePickerDialog 주요 활동의 변수를 가져 오는 조각입니다.

// Activity Method 

public void showDatePickerDialog(View v) { 
    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getFragmentManager(), "datePicker"); 
    firstDatem = ((DatePickerFragment) newFragment).getMonth(); 
    firstDated = ((DatePickerFragment) newFragment).getDay(); 
    firstDatey = ((DatePickerFragment) newFragment).getYear(); 
} 

// DatePickerDialog 

private int year; 
private int month; 
private int day; 


public void onDateSet(DatePicker view, int year, int month, int day) { 
    // do some stuff for example write on log and update TextField on activity 
    String monthstring; 
    switch (month) { 
     case 0: monthstring = "January"; 
       break; 
     case 1: monthstring = "February"; 
       break; 
     case 2: monthstring = "March"; 
       break; 
     case 3: monthstring = "April"; 
       break; 
     case 4: monthstring = "May"; 
       break; 
     case 5: monthstring = "June"; 
       break; 
     case 6: monthstring = "July"; 
       break; 
     case 7: monthstring = "August"; 
       break; 
     case 8: monthstring = "September"; 
       break; 
     case 9: monthstring = "October"; 
       break; 
     case 10: monthstring = "November"; 
       break; 
     case 11: monthstring = "December"; 
       break; 
     default: 
       monthstring = ""; 
       break; 
    } 
    ((TextView) getActivity().findViewById(R.id.date_text)).setText("First date set to " + monthstring + " " + day + ", "+ year); 
} 
public int getYear() 
{ 
    return year; 
} 
public int getMonth() 
{ 
    return month; 
} 
public int getDay() 
{ 
    return day; 
} 
} 
+0

http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment. 인터페이스를 콜백으로 사용하여 – Raghunandan

답변

0

MainActivity에 대한 DatePickerDialog 콜백이 있어야합니다. 그것은 훨씬 더 쉽고 깨끗해질 것입니다.

이 같은 DatePickerDialogFragment 만들기 :

public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener 
{ 
    // This interface is implemented by this fragment's container activity. 
    // In your case, it will be implemented by MainActivity. 
    // When the container activity creates an instance of this fragment, it passes in a reference to this 
    // method. This allows the current fragment to use that reference to pass the entered date values back 
    // to that container activity as soon as they are entered by the user. 
    public static interface DatePickedListener 
    { 
     public void onDatePicked(int selectedYear, int selectedMonth, int selectedDay); 
    } 

    private DatePickedListener listener; 


    @Override 
    public void onAttach(Activity activity) 
    { 
     // when the fragment is initially attached to the activity, 
     // cast the activity to the callback interface type 
     super.onAttach(activity); 

     try 
     { 
      listener = (DatePickedListener) activity; 
     } 
     catch (ClassCastException e) 
     { 
      throw new ClassCastException(activity.toString() + 
       " must implement " + DatePickedListener.class.getName()); 
     } 
    } 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 

     // the activity passes in these arguments when it 
     // creates the dialog. use them to create the dialog 
     // with these initial values set 
     Bundle b = getArguments(); 

     int year = b.getInt("set_year"); 
     int month = b.getInt("set_month"); 
     int day = b.getInt("set_day"); 

     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 


    @Override 
    public void onDateSet(DatePicker view, int setYear, int setMonth, int setDay) 
    { 
     // when the date is selected, immediately send it to the activity via 
     // its callback interface method 

     listener.onDatePicked(setYear, setMonth, setDay); 
    } 

} 

이 MainActivity가 DatePickerDialogFragment.DatePickedListener 인터페이스를 구현 가지고 위의 DatePickerDialogFragment에 을 정의, 다음이 패턴은 몇 가지 의견과 함께 작동하는 방법의 예입니다. 그런 다음 사용자가 이벤트 처리기에서이 같은 뭔가를, 날짜 선택을 표시 버튼을 누르면 해당 MainActivity에서 : 사용자가 날짜를 선택합니다

Calendar cal = Calendar.getInstance(); 

Bundle b = new Bundle(); // create a bundle object to pass currently set date to DatePickerDialogFragment 

b.putInt("set_year", cal.get(Calendar.YEAR)); 
b.putInt("set_month", cal.get(Calendar.MONTH)); 
b.putInt("set_day", cal.get(Calendar.DAY_OF_MONTH)); 

// show the date picker fragment (which contains a date picker dialog) 
DialogFragment datePickerDialogFragment = new DatePickerDialogFragment(); 
datePickerDialogFragment.setArguments(b); // set the bundle on the DatePickerDialogFragment   
datePickerDialogFragment.show(getSupportFragmentManager(), TAG_DATE_PICKER_FRAGMENT); 

, onDateSet은 당신의 날짜 선택 조각에 호출됩니다 은 MainActivity에서 onDatePicked()를 호출하는 listener.onDatePicked (setYear, setMonth, setDay)를 호출합니다. 따라서 MainActivity에서 에 게터 메서드를 사용하지 않아도됩니다. 결과는 사용자가 대화 상자의 날짜를 완료하면 자동으로 반환됩니다.

+0

캘린더에서 날짜를 얻은 방법을 생략했습니다. – user3814742

+0

Google picker 섹션의 onCreateDialog 메소드와 거의 같습니다 : http://developer.android.com/guide/topics/ui/controls/pickers.html – user3814742