I'm new in programming and android. I'm having problem with a datePickerfragment. Error when implementing DatePickerFragment.OnFragmentInteractionListener on mainActivity, it says that "Class MainActivity must either be declared abstract or implement abstract method 'returnDate(String)' in 'OnFragmentInteractionListener'."
I used the datePicker as shown in http://developer.android.com/guide/topics/ui/controls/pickers.html.
The datePicker works but I want to transfer the value selected to mainActivity and I took some tips in this other post: How to transfer the formatted date string from my DatePickerFragment?.
I don't really know what I'm missing. I would also like to know if its possible to transfer de Date value instead of a String value. I wanted to manipulate the Date in mainActivity. Hope someone can help me. Here is my code:
package com.example.android.datepickertest;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends FragmentActivity
implements DatePickerFragment.OnFragmentInteractionListener
{
private TextView dataSelecionada;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSelecionada = (TextView)findViewById(R.id.data_selecionada);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
@Override
public void setTextDate (int year, int month, int day) {
TextView displayDataSelecionada = (TextView) findViewById(R.id.data_selecionada);
dataSelecionada.setText(day + "/" + month +"/" + year);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.android.datepickertest;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link DatePickerFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link DatePickerFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
OnFragmentInteractionListener listener;
public interface OnFragmentInteractionListener {
public void returnDate(String date);
};
@Override
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);
listener=(OnFragmentInteractionListener)getActivity();
// 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) {
// Do something with the date chosen by the user
Calendar c= Calendar.getInstance();
c.set(year,month,day);
SimpleDateFormat date = new SimpleDateFormat();
String dateSet = date.format(c.getTime());
if (listener !=null){
listener.returnDate(dateSet);
}
}
}
The error has the answer, when you implement an interface, you should implement all the methods in the interface.
In your case there is no method returndate in your mainactivity.
1.Create the method returnDate or if you use Android studio, just go to file MainActivity and put your cursor over 'public class MainActivity' then press Alt+Enter, the method ll be automatically created, and you ll the date in your mainactivity through this method.
new Date (c.getTime())