我有两个TextView。每个人都有一个听众。按下时会打开MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener
。选择日期后,它将设置为所需的TextView。我将TextView作为参数传递给构造函数。我需要改变它。我被建议使用接口回调将结果发送到调用活动。但我无法弄明白。如何确定TextView粘贴文本的内容?
请帮我解释一下我的例子。
我的代码:
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;
和
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);
}
}
我尝试了示例How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? 但无法申请其代码。如何确定TextView插入结果的内容。我倾向于创建两个类MyDatePicer。 可以展示我的例子如何实现它?
完整课程:
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;
}
}
答案 0 :(得分:1)
在日期选择器类中,您希望实现一个简单的界面。
public class MyDatePicer extends DialogFragment implements DatePickerDialog.OnDateSetListener {
Bundle bundle;
String myString
// Interface
MyDatePicerListener mListener;
public interface MyDatePicerListener {
public abstract void setDateBegin(String log);
public abstract void setDateEnd(String log);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
}
// This is he missing component
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (MyDatePicerListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement Listener");
}
}
@Override
public void onStart() {
bundle = this.getArguments();
myString = bundle.getString("date", "");
}
@Override
public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
@Override
public void onDateSet(android.widget.DatePicker datePicker, int year,int month, int day) {
if (myString.equals("start")){
mListener.setDateStart(day + "-" + (month + 1) + "-" + year);
}
if (myString.equals("end")){
mListener.setDateEnd(day + "-" + (month + 1) + "-" + year);
}
}
}
在带有textViwew的活动中
public class EstimatedLoad extends Fragment implements View.OnClickListener, MyDatePicker.MyDatePickerListener {
private TextView dateBegin;
private TextView dateEnd;
private TableLayout holidayTable;
private TableLayout cityayTable;
private TextView cityHeader;
private TextView holidayHeader;
// DatePickerFragment fragment;
Bundle bundle;
@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);
fragment = new DatePickerFragment();
bundle = new Bundle();
return v;
}
@Override
public void onClick(View v) {
DialogFragment dateDialog;
switch (v.getId()){
case R.id.dateBegin:
dateDialog = new MyDatePicer(dateBegin);
bundle.putString("date", "start");
//dateDialog.setArguments(bundle);
dateDialog.setArguments(bundle);
dateDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
break;
case R.id.dateEnd:
dateDialog = new MyDatePicer(dateEnd);
bundle.putString("date", "end");
// fragment.setArguments(bundle);
dateDialog.setArguments(bundle);
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;
}
}
@Override
public void setDateBegin(String log) {
// example
dateBegin.setText(log);
}
@Override
public void setDateEnd(String log) {
// example
dateEnd.setText(log);
}