0

I am using a datepicker to select my date, after which, it will store into my database as String.

Can I know how do I take out the date in my database as a String?

This is because, I need to do a summary page, that pass all the data into the next intent. Meaning, I need to pass the data from my view all into my summary page, in order to have all the data in summary page. This is to allow me to be able to filter our the date easily.

For example, if date - 27/12/2014, it will show the list of result.

I did a quick check on my data, to see if I'm able to check the result. I've put a datepicker in my summary.java, which is in the editText(monthDate), so if monthDate = bundleDate(which is all the data result that I pass from view all), it will start a new intent. Else, it will go to about page.

However, my code skips the IF part, even if I select the right date, it will go through the else if code and not the else code.

public class summary extends Activity {

    static EditText monthDate;
    TextView avgPrice;
    TextView exFuel;
    TextView avgFC;
    Button doneButton;
    Button exitButton;
       private String bundleID;
       private String bundleDate;
        private String bundlePrice;
        private String bundlePump;
        private String bundleCost;
        private String bundleOdometer;
        private String bundlePreOdometer;

        private String bundleFcon;
        static final int DATE_DIALOG_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary);

        monthDate = (EditText)findViewById(R.id.month);
        avgPrice = (TextView)findViewById(R.id.showavfPriceTV);
        exFuel = (TextView)findViewById(R.id.showexFuelTV);
        avgFC = (TextView)findViewById(R.id.showavgFCTV);
        doneButton = (Button)findViewById(R.id.doneBTN);
        exitButton = (Button)findViewById(R.id.exitBTN);

        Bundle takeBundledData = getIntent().getExtras();  
        // First we need to get the bundle data that pass from the UndergraduateListActivity

        bundleID = takeBundledData.getString("clickedID");
        bundleDate = takeBundledData.getString("clickedDate");
        bundlePrice = takeBundledData.getString("clickedPrice");
        bundlePump = takeBundledData.getString("clickedPump");
        bundleCost = takeBundledData.getString("clickedCost");
        bundleOdometer = takeBundledData.getString("clickedOdometer");
        bundlePreOdometer = takeBundledData.getString("clickedpreOdometer");
        bundleFcon = takeBundledData.getString("clickedFCon");

        Log.d("SUMMARY","bundle : "+ takeBundledData);

        monthDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               // showDialog(DATE_DIALOG_ID);
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });
        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (monthDate.equals(bundleDate))
                {
                    Log.d("TRYINGOUT","date : "+ bundleDate);
                     Intent intent=new Intent(getApplicationContext(),homeActivity.class);
                     startActivity(intent);
                }
                else 
                {
                     Intent intent=new Intent(getApplicationContext(),about.class);
                     startActivity(intent);
                }

            }
        });
}


    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;

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

    //return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

    // 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) {

        monthDate.setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
        // set selected date into datepicker also
}}}
Chloe
  • 149
  • 1
  • 12

1 Answers1

1

It doesn't work because you try to compare Object with string. .equals() doesnt check variable compatibilty. What you need to do, is to get text from your EditText and compare it with your bundleDate

Also remember that EditText.getText() method returns Editable so you need to convert it to string.

Change your condition into:

//if(monthDate.equals(bundleDate))
if (monthDate.getText().toString().equals(bundleDate))
 {
    Log.d("TRYINGOUT","date : "+ bundleDate);
    Intent intent=new Intent(getApplicationContext(),homeActivity.class);
    startActivity(intent);
}
//rest of your code

and make sure your Date stored in bundle and which you enter into EditText has the same format, i.e. dd-MM-yyyy

@EDIT If you want to compare only months, and you are pretty sure date stored in bundle will always have the same format (dd/MM/yyyy as you mentioned), then you can extract substring from your date string

1) by using subSequence(start, stop) - this will return string created from chars on position 3 and 4 of your string

//if(monthDate.equals(bundleDate))
if (monthDate.getText().toString().equals(bundleDate.subSequence(3,5).toString()))
 {
    //your code
}
//rest of your code

2) by using String.split()

prepare array to keep split result String[] dateParts = bundleDate.split("/"); - this will return 3 strings to your array, first will keep day, second will keep month, and third will keep year

so in code use second one (index starts from zero)

String[] dateParts = bundleDate.split("/");
//if(monthDate.equals(bundleDate))
if (monthDate.getText().toString().equals(dateParts[1]))
 {
    //your code
}
//rest of your code

This should work

Lonti84
  • 202
  • 1
  • 6
  • can I check, you was saying that, "Also remember that EditText.getText() method returns Editabe so you need to convert it to string." How can I change to String – Chloe Jan 28 '14 at 11:05
  • can you help me with this? – Chloe Jan 28 '14 at 11:17
  • http://stackoverflow.com/questions/21383772/passing-a-list-of-data-from-one-intent-to-another-using-serializable – Chloe Jan 28 '14 at 11:17
  • Hi, can I check, if I just want to take out the MONTH for it, how can I do so? – Chloe Jan 28 '14 at 11:34
  • @Chloe just to make sure i understood everything well: the only thing you want to compare is MONTH from your bundleDate with MONTH entered inside your EditText? – Lonti84 Jan 28 '14 at 11:47
  • yes, however, everything that I've stored in the database is dd/MM/yyyy – Chloe Jan 28 '14 at 11:49
  • you mean, either one also able to use is it? – Chloe Jan 28 '14 at 12:14