0

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;
    }
}
Community
  • 1
  • 1
Valera Valerianov
  • 247
  • 1
  • 3
  • 14

1 Answers1

1

Within the date picker class, you want to implement a simple interface.

 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);
    }
    }
}

Within the Activity with the 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);
    }
Eugene H
  • 3,520
  • 3
  • 22
  • 39
  • So the problem is that I do not know what TextView (dateBegin or DateEnd) text set – Valera Valerianov Dec 23 '14 at 06:29
  • Okay, then you want to send some sort of notification to the Dialog fragment indicating whether the begin or end was clicked? – Eugene H Dec 23 '14 at 06:31
  • I will put something together for you really fast. One second – Eugene H Dec 23 '14 at 06:35
  • I have 2 TextView. Click on the first (start date) - select a date - Click Finish - the date is set. Click on the second (end date) - select a date - Click Finish - the date is set. I need to know which of them to set the date – Valera Valerianov Dec 23 '14 at 06:36
  • yes yes is almost what I need. just write in the method setDate(String log) in class EstimatedLoad ? – Valera Valerianov Dec 23 '14 at 06:47
  • @ValeraValerianov Have a look at what I just did. Within the OnClick – Eugene H Dec 23 '14 at 06:48
  • Yes, I have just started to do)))) your answer is the best! Thanks for your time! Now I will check, but I think the right thing. – Valera Valerianov Dec 23 '14 at 06:53
  • Okay. Let me know if it works. If it doesn't we can mess around with the code a tad bit to get it working correctly. There may be some issues with the communication from the fragment to the dialog fragment. – Eugene H Dec 23 '14 at 06:55
  • And you may need to put the bundle = this.getArguments(); myString = bundle.getString("date", ""); within the onCreate. – Eugene H Dec 23 '14 at 06:57
  • java.lang.NullPointerException at com.managment.pavel.managmentgradle.fragments.MyDatePicer.onDateSet(MyDatePicer.java:68) this is the time when you press the button "Finish" on this line: mListener.setDateEnd(day + "-" + (month + 1) + "-" + year); – Valera Valerianov Dec 23 '14 at 07:12
  • @ValeraValerianov Did you implement Both; public interface MyDatePicerListener { public abstract void setDateBegin(String log); public abstract void setDateEnd(String log); } – Eugene H Dec 23 '14 at 07:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67518/discussion-between-valera-valerianov-and-eugene-h). – Valera Valerianov Dec 23 '14 at 07:21
  • @ValeraValerianov Hey I just started a new project and wanted to work with fragments and I came across your problem. We didnt initiate the listener in the onAttach() method. That is why we were getting null pointer. It should be a complete answer now. It should work. If not let me know. – Eugene H Dec 30 '14 at 18:35