-1

THIS MY CODE

package com.clip.android;
/**
 * 
 * @author abhishek
 *
 */

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;


public class ClaimRegister extends ClaimActivity implements OnClickListener{

    EditText polid;
    /*DatePicker claimDate;*/
    String userValue;
    String getClaimDate;
    Spinner Spinner;
    String clmType;
    ImageButton calIcon;
    EditText claimDate;
    /*TextView b1;*/
    double a = 4.32;
    double b = Math.pow(10.0,8.0);
    int year,day,month;
    List<String> claimTypes=new ArrayList<String>();
/*    List<Customer> customers = new ArrayList<Customer>();*/


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_claim_register);
        setHeader("Register Your Claim", true, true);
        claimDate = (EditText)findViewById(R.id.editText1);
        polid = (EditText)findViewById(R.id.polid);
        /*claimDate = (DatePicker)findViewById(R.id.dp);*/
        Spinner=(Spinner) findViewById(R.id.spin);
        userValue = polid.getText().toString();
        calIcon = (ImageButton)findViewById(R.id.imageButton1); 
        calIcon.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Calendar c = Calendar.getInstance();

                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);
                /*claimDate.setMinDate((long)(c.getTimeInMillis()-(a*b)));
                claimDate.init(year, month, day, null);*/

                 claimDate.setText(String.valueOf(c.getTimeInMillis()-(a*b)));


                DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener()  {
                     public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
                        {   
                             Calendar cl = Calendar.getInstance();
                                cl.set(year, monthOfYear, dayOfMonth-1);
                                if(cl.before(Calendar.getInstance())){
                                    claimDate.setText(dayOfMonth+"-"+(monthOfYear)+"-"+year);
                                }else {
                                    claimDate.setText("");
                                    Toast.makeText(ClaimRegister.this, "Date Shold Be Within Current Date.",    Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                year = year; 
                                month = monthOfYear; 
                                day = dayOfMonth;

                        }
                }, year, month, day;
        ((Dialog) datePickerListener).show();

               }
            });
    AsyncCall drop = new AsyncCall();
    drop.execute();
    }

    private class AsyncCall extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... params) {
            Log.d("doBack", "0000");
            claimTypes = com.clip.webservice.GetAQuote.GetLineofBusiness("CLMTYPE", "getClmType");


            return null;
        }

        protected void onPostExecute(Void result) 
        {
            Log.d("onPost", "lol");
                    ArrayAdapter<String> adapter =  new ArrayAdapter<String>(ClaimRegister.this, android.R.layout.simple_spinner_dropdown_item,claimTypes);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    Log.d("onPost", "123");
                    Spinner.setAdapter(adapter);
                    Log.d("onPost", "hiiii");
                    Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {
                                String item = parent.getItemAtPosition(position).toString();
                                Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                                clmType= Spinner.getSelectedItem().toString();
                        }

                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub
                            String item=parent.getSelectedItem().toString();
                            Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                            clmType= Spinner.getSelectedItem().toString();
                        }

                    });

                     }
                    }


    public void submit(View view)
    {
        userValue = polid.getText().toString();

        if(userValue.trim().equals("")&& userValue != null){
            Toast.makeText(this, "Policy Id is required", Toast.LENGTH_LONG).show();
        }
        else{
        Intent intent = new Intent(this,ClaimRegisterPage.class);
        intent.putExtra("getclmType", clmType);
        intent.putExtra("userValue", userValue);
        Log.d("In submit", "userValue = "+userValue);
        startActivity(intent);
        }
    }
}

ERROR:-

04-26 11:45:04.450: E/AndroidRuntime(2813): FATAL EXCEPTION: main
04-26 11:45:04.450: E/AndroidRuntime(2813): Process: com.clip.android, PID: 2813
04-26 11:45:04.450: E/AndroidRuntime(2813): java.lang.ClassCastException: com.clip.android.ClaimRegister$1$1 cannot be cast to android.app.Dialog
04-26 11:45:04.450: E/AndroidRuntime(2813):     at com.clip.android.ClaimRegister$1.onClick(ClaimRegister.java:96)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at android.view.View.performClick(View.java:5198)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at android.view.View$PerformClick.run(View.java:21147)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at android.os.Handler.handleCallback(Handler.java:739)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at android.os.Looper.loop(Looper.java:148)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at android.app.ActivityThread.main(ActivityThread.java:5417)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at java.lang.reflect.Method.invoke(Native Method)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-26 11:45:04.450: E/AndroidRuntime(2813):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

What Set current date as default date and i want to allow user 15 days behind from current date not below than that date.Please help to resolve this.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
Abhishek
  • 79
  • 2
  • 15

4 Answers4

2

DatePickerDialog is a Dialog But DatePickerDialog.OnDateSetListener is not a Dailog that can be cast. So show DatePickerDialog instead of showing datePickerListener. It's the line of error

 ((Dialog) datePickerListener).show();

See the example how DatePickerDialog is created and used.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • that's I got it but is there is way to resolve this issue – Abhishek Apr 26 '16 at 06:34
  • The example I showed is so simple that you can easily understand, But your code seems too complex. You can easily show dialog by using `showDialog(id)`. – Shree Krishna Apr 26 '16 at 06:40
  • But i want to allow user to set till previous 15 days from current date not more than that.How can do this ???Shree – Abhishek Apr 26 '16 at 07:16
  • @Abhishek see [this](http://stackoverflow.com/questions/17878751/how-to-disable-dates-before-today-date-in-datepickerdialog-android). You might find it helpful – Shree Krishna Apr 26 '16 at 08:33
  • I Resolved my Issue Thanks EveryOne....I also Posted my resolved code below It works fine.......... – Abhishek Apr 27 '16 at 05:26
0
//Current date
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

// editText is the EditText  that should display it
editText.setText(currentDateTimeString);

For 15 days limit, you can use if else condition before setting the date to edit text.So if user Selects date before 15 days the edit text remains empty

Jhaman Das
  • 1,094
  • 13
  • 28
Pankaj Kumar
  • 71
  • 1
  • 1
  • 10
0

There are couple of errors that I've noticed in the code, 1) Replace

calIcon.setOnClickListener(new Button.OnClickListener()

with

calIcon.setOnClickListener(new View.OnClickListener() 

2) Below type-casting is causing the crash

 ((Dialog) datePickerListener).show();

You should create an instance of Dialog, build it with require attributes and set the listener. Then call the show() method on it. Refer, Datepicker: How to popup datepicker when click on button and store value in variable

Community
  • 1
  • 1
user_1989
  • 375
  • 4
  • 10
0

Resolved Code

package com.clip.android;
/**
 * 
 * @author abhishek
 *
 */

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;


public class ClaimRegister extends ClaimActivity implements OnClickListener{

    EditText polid;
    String userValue;
    String getClaimDate;
    Spinner Spinner;
    String clmType;
    ImageButton calIcon;
    EditText claimDate;
    double a = 1.296;
    double b = Math.pow(10.0,9.0);

    static final int DATE_PICKER_ID = 0; 
    int year,day,month;
    List<String> claimTypes=new ArrayList<String>();



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_claim_register);
        setHeader("Register Your Claim", true, true);
        claimDate = (EditText)findViewById(R.id.editText1);
        polid = (EditText)findViewById(R.id.polid);
        Spinner=(Spinner) findViewById(R.id.spin);
        userValue = polid.getText().toString();
        calIcon = (ImageButton)findViewById(R.id.imageButton1); 
        claimDate.setEnabled(false);

        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);


        claimDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(day).append("-").append(month + 1).append("-")
        .append(year).append(" "));
        calIcon.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            public void onClick(View v) {

                // On button click show datepicker dialog 


                showDialog(DATE_PICKER_ID);
            }

        });


    AsyncCall drop = new AsyncCall();
    drop.execute();
    }

    private class AsyncCall extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... params) {
            Log.d("doBack", "0000");
            claimTypes = com.clip.webservice.GetAQuote.GetLineofBusiness("CLMTYPE", "getClmType");


            return null;
        }

        protected void onPostExecute(Void result) 
        {
            Log.d("onPost", "lol");
                    ArrayAdapter<String> adapter =  new ArrayAdapter<String>(ClaimRegister.this, android.R.layout.simple_spinner_dropdown_item,claimTypes);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    Log.d("onPost", "123");
                    Spinner.setAdapter(adapter);
                    Log.d("onPost", "hiiii");
                    Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                        public void onItemSelected(AdapterView<?> parent,
                                View view, int position, long id) {
                                String item = parent.getItemAtPosition(position).toString();
                                Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                                clmType= Spinner.getSelectedItem().toString();
                        }

                        public void onNothingSelected(AdapterView<?> parent) {
                            // TODO Auto-generated method stub
                            String item=parent.getSelectedItem().toString();
                            Toast.makeText(getApplicationContext(), "type = "+ item, Toast.LENGTH_LONG).show();
                            clmType= Spinner.getSelectedItem().toString();
                        }

                    });

                     }
                    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_ID:

            // open datepicker dialog. 
            // set date picker for current date 
            // add pickerListener listner to date picker
            //disabled the dates lesser than 15 days from current date onload of date picker dialog 
            DatePickerDialog dp = new DatePickerDialog(this, pickerListener, year, month,day);
                Calendar c = Calendar.getInstance();
                dp.getDatePicker().setMinDate(((long)(c.getTimeInMillis()-(a*b))));
                return dp;
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
              year  = selectedYear;
              month = selectedMonth;
              day   = selectedDay;

            // Show selected date 

            claimDate.setText(new StringBuilder().append(day)
                    .append("-").append(month + 1).append("-").append(year)
                    .append(" "));

           }
        };


    public void submit(View view)
    {
        userValue = polid.getText().toString();
            //Validation for Policy Id since it's mandatory
        if(userValue.trim().equals("")&& userValue != null){
            Toast.makeText(this, "Policy Id is required", Toast.LENGTH_LONG).show();
        }
        else{
        Intent intent = new Intent(this,ClaimRegisterPage.class);
        intent.putExtra("getclmType", clmType);
        intent.putExtra("userValue", userValue);
        Log.d("In submit", "userValue = "+userValue);
        startActivity(intent);
        }
    }
}
Abhishek
  • 79
  • 2
  • 15