I have two TextView. each has a listener. when pressed opens MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener
. after selecting the date it is set to the desired TextView. I pass the TextView as a parameter in the constructor. I need to change it. I was advised to use an interface callback to send the result to the calling activity. But I can not figure it out. How to determine what the TextView paste text?
Please help me on my example.
MY CODE:
case R.id.dateBegin:
dateDialog = new MyDatePicer(dateBegin);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
case R.id.dateEnd:
dateDialog = new MyDatePicer(dateEnd);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
and:
public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private TextView tv;
public MyDatePicer(TextView tv) {
this.tv = tv;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
}
@Override
public void onStart() {
}
@Override
public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
tv.setText(day + "-" + (month + 1) + "-" + year);
}
}
I tried the example How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? but is not able to apply to its code. How to determine what the TextView to insert the result. I'm leaning towards the creation of two classes MyDatePicer. could show my example how to implement it?
FULL CLASS:
public class EstimatedLoad extends Fragment implements View.OnClickListener {
private TextView dateBegin;
private TextView dateEnd;
private TableLayout holidayTable;
private TableLayout cityayTable;
private TextView cityHeader;
private TextView holidayHeader;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.estimated_load, null);
dateBegin = (TextView) v.findViewById(R.id.dateBegin);
dateEnd = (TextView) v.findViewById(R.id.dateEnd);
dateBegin.setOnClickListener(this);
dateEnd.setOnClickListener(this);
Button load = (Button) v.findViewById(R.id.btnLoadDate);
load.setOnClickListener(this);
holidayTable = (TableLayout) v.findViewById(R.id.hhtable);
cityayTable = (TableLayout) v.findViewById(R.id.chtable);
holidayHeader = (TextView) v.findViewById(R.id.hhHeaderLoad);
cityHeader = (TextView) v.findViewById(R.id.chHeaderLoad);
holidayHeader.setVisibility(View.INVISIBLE);
cityHeader.setVisibility(View.INVISIBLE);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
dateBegin.setText(year + "-" + (month + 1) + "-" + day);
dateEnd.setText(year + "-" + (month + 1) + "-" + c.getActualMaximum(Calendar.DAY_OF_MONTH));
return v;
}
@Override
public void onClick(View v) {
DialogFragment dateDialog;
switch (v.getId()){
case R.id.dateBegin:
dateDialog = new MyDatePicer(dateBegin);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
case R.id.dateEnd:
dateDialog = new MyDatePicer(dateEnd);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
case R.id.btnLoadDate:
if (InternetResiver.isOnline(getActivity())) {
clearTable(holidayTable);
clearTable(cityayTable);
holidayHeader.setVisibility(View.VISIBLE);
cityHeader.setVisibility(View.VISIBLE);
setData(); } else {
AlertDialog alert = InternetResiver.getAlertDialog(getActivity());
alert.show();
}
break;
}
}
private void clearTable(TableLayout table) {
int count = table.getChildCount();
for (int i = 0; i < count; i++) {
View child = table.getChildAt(i);
if (child instanceof TableRow) ((ViewGroup) child).removeAllViews();
}
}
public void setData(){
new MyAsincTask(getActivity()){
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
ArrayList<EstimatedLoadBean> estimatedLoadBeansHH = new ArrayList<>();
ArrayList<EstimatedLoadBean> estimatedLoadBeansCH = new ArrayList<>();
Elements documentHH = document.select("holiday");
Elements eNameHH = documentHH.select("name");
Elements eValueHH = documentHH.select("value");
for (int i = 0; i < eNameHH.size(); i++) {
EstimatedLoadBean estimatedLoadBeanHH = new EstimatedLoadBean();
estimatedLoadBeanHH.setName(eNameHH.get(i).ownText());
estimatedLoadBeanHH.setValue(eValueHH.get(i).ownText());
estimatedLoadBeansHH.add(estimatedLoadBeanHH);
}
Elements documentCH = document.select("city");
Elements eNameCH = documentCH.select("name");
Elements eValueCH = documentCH.select("value");
for (int i = 0; i < eNameCH.size(); i++) {
EstimatedLoadBean estimatedLoadBeanCH = new EstimatedLoadBean();
estimatedLoadBeanCH.setName(eNameCH.get(i).ownText());
estimatedLoadBeanCH.setValue(eValueCH.get(i).ownText());
estimatedLoadBeansCH.add(estimatedLoadBeanCH);
}
TableRow.LayoutParams row_params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.1f);
for (EstimatedLoadBean hhbean : estimatedLoadBeansHH) {
TableRow tableRow = getTableRowData(row_params, hhbean);
holidayTable.addView(tableRow);
}
for (EstimatedLoadBean chbean : estimatedLoadBeansCH) {
TableRow tableRow = getTableRowData(row_params, chbean);
cityayTable.addView(tableRow);
}
}
}.execute("......key=kitkat&type=xml&tel=0555555555&action=tell_me_about_occupation&date_start="+dateBegin.getText()+"&date_end="+dateEnd.getText()+"");
}
private TableRow getTableRowData(TableRow.LayoutParams row_params, EstimatedLoadBean bean) {
TableRow tableRow = new TableRow(getActivity());
TextView name = new TextView(getActivity());
TextView value = new TextView(getActivity());
name.setTextColor(Color.BLACK);
value.setTextColor(Color.BLACK);
name.setText(bean.getName());
value.setText(bean.getValue());
Typeface type= Fonts.getSubHeaderFont(getActivity());
name.setTypeface(type);
value.setTypeface(type);
name.setPadding(5,0,0,0);
tableRow.addView(name, row_params);
tableRow.addView(value, row_params);
return tableRow;
}
}