B. Dee B. Dee - 4 months ago 14
Android Question

Two different onClick for a different Dialog for DatePicker

I have two different edittext for

reservation
and
return
and I want it to have a different Date Picker but when I'm over riding a method it says
Method does not override method from its super class
here is my code.

package com.example.kun.carkila;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;

import java.util.Calendar;
import java.util.HashMap;

public class TransactionActivity extends AppCompatActivity {

EditText etResDate, etReturnDate ;
Button btnRent;
int year_x,month_x,day_x;
int year_x1,month_x1,day_x1;
int hour_x,minute_x;

static final int DIALOG_ID = 0;
static final int DIALOG_ID1 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transaction);
showDialogOnButtonClick();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
year_x1 = cal.get(Calendar.YEAR);
month_x1 = cal.get(Calendar.MONTH);
day_x1 = cal.get(Calendar.DAY_OF_MONTH);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);


etResDate = (EditText) findViewById(R.id.etResDate);
etReturnDate = (EditText) findViewById(R.id.etReturnDate);
btnRent = (Button) findViewById(R.id.btnRent);

btnRent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("txtResDate", etResDate.getText().toString());
postData.put("txtReturnDate", etReturnDate.getText().toString());

PostResponseAsyncTask taskRent = new PostResponseAsyncTask(TransactionActivity.this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
if(s.contains("Rented")){
Toast.makeText(TransactionActivity.this,"Car Rented!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(TransactionActivity.this, ListActivity.class);
startActivity(in);
finish();
}
}
});
taskRent.execute("http://carkila.esy.es/transaction.php");
}
});
}

public void showDialogOnButtonClick() {
etResDate = (EditText) findViewById(R.id.etResDate);
etReturnDate = (EditText) findViewById(R.id.etReturnDate);

etResDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
});

etReturnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID1);
}
});

}

@Override
protected Dialog onCreateDialog(int id){
if(id == DIALOG_ID){
return new DatePickerDialog(this, dplistener, year_x,month_x,day_x);
}
else if (id == DIALOG_ID1){
return new DatePickerDialog(this, dplistener1, year_x1, month_x1, day_x1);
}
return null;
}

private DatePickerDialog.OnDateSetListener dplistener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
etResDate.setText(month_x + "/" + day_x + "/" + year_x);
}

};
private DatePickerDialog.OnDateSetListener dplistener1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year1, int monthOfYear1, int dayOfMonth1) {
year_x1 = year1;
month_x1 = monthOfYear1 + 1;
day_x1 = dayOfMonth1;
etReturnDate.setText(month_x1 + "/" + day_x1 + "/" + year_x1);
}
};


}


Thanks for the help guys. :)

Answer

I guess that correct is onCreateDialog() and not onCreateDialog1()

Maybe, you just need to delete onCreateDialog1() and update your onCreateDialog() as follows:

@Override
protected Dialog onCreateDialog(int id){
    if(id == DIALOG_ID){
        return new DatePickerDialog(this, dplistener, year_x,month_x,day_x);
    else if (id == DIALOG_ID1) {
        return new DatePickerDialog(this, dplistener1, year_x, month_x, day_x);
    }
    return null;
}

UPDATE

Another error is here:

static final int DIALOG_ID = 0;
static final int DIALOG_ID1 = 0;

Both dialogs has the SAME ID. You must set DIALOG_ID1 = 1;