Reputation: 13
I'd like to set a date interval, and when the user presses the end date, draw a chart. The problem i have is that the chart is drawn both on the start and the end date.
I've tried to set a boolean variable or an int to decide when the chart should be drawn, but as i'm using "onDateSet" from "DatePickerFragmet", i cannot modify its parameters... I paste my code here to help clarify what i'm trying to explain:
On my main activity i have:
startDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i=0; i<2; i++){
showDatePickerDialog(v,i);
}
}
});
public void showDatePickerDialog(View v, int i) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
Then on a new class i call onDateSet:
public class DatePickerFragment 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
LineGraph lineGraph = new LineGraph();
lineGraph.run(getActivity());
}
... How could i draw the chart when the final date is selected?
Upvotes: 1
Views: 1975
Reputation: 41
Write a if..else or a switch statement based on the Tag passed in the newFragment.show();
Here you will have to write a showDatePickerDialog()
method for every date picker separately so that each hold a different tag.
I have implemented it as follows:
public void showDeadlineDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "deadlineDate");
}
public void onDateSet(DatePicker view, int year, int month, int day) {
StringBuilder task = new StringBuilder();
task.append(day);
task.append(".");
task.append(month+1);
task.append(".");
task.append(year);
String x=task.toString();
if (getTag()=="deadlineDate")
{
btn=(Button) getActivity().findViewById(R.id.btn_deadline_date);
btn.setText(x);
}
if (getTag()=="reminderDate")
{
btn=(Button) getActivity().findViewById(R.id.btn_reminder_date);
btn.setText(x);
}
}
Upvotes: 3
Reputation: 13
As I can't find a way to do this as I mentioned before, I've reached a solution by having 2 buttons (start and end) to select the dates, instead of only havin one and use a loop. After that, i created another class, and each button calls a different class.
Declaration of the buttons and the methods "showStartDatePicker" and "showEndDatePicker":
final Button startDate = (Button)findViewById(R.id.startDate);
final Button endDate = (Button)findViewById(R.id.endDate);
public void showStartDatePicker(View v) {
DialogFragment newFragment = new StartDatePickerFragment();
newFragment.show(getFragmentManager(), "StartDatePicker");
}
public void showEndDatePicker(View v) {
DialogFragment newFragment = new EndDatePickerFragment();
newFragment.show(getFragmentManager(), "EndDatePicker");
}
Class "StartDatePickerFragment":
public class StartDatePickerFragment 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
Toast.makeText(getActivity(), "Fecha seleccionada: " + day + "/" + (month+1) + "/" + year, Toast.LENGTH_SHORT).show();
}
}
Class "EndDatePickerFragment":
public class EndDatePickerFragment 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) {
LineGraph lineGraph = new LineGraph();
lineGraph.run(getActivity());
}
}
I'm almost doing the same thing twice (creating the DatePicker), but I can't find a more efficient way.
Upvotes: 0