I'm developing an app which will prompt a user a DatePickerDialog, after the user insert the date i'll manage the date and add it to a listview. So far i have achieved:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_somministrazione);
Button Aggiungi;
ListView ListaSomminisrazione;
Aggiungi = (Button) findViewById(R.id.button3);
ListaSomminisrazione = (ListView)findViewById(R.id.listView2);
final ArrayList<String> ris = new ArrayList<String>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Somministrazione.this,android.R.layout.simple_list_item_1 , ris);
final Database db=new Database(getApplicationContext());
Aggiungi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
//WAIT FOR THE DIALOG TO DISMISS IN ORDER TO ADD THE GIVEN DATE CHOSEN BY THE USER INTO THE ListView ListaSomminisrazione;
}
});
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
int year2,month2,day2;
@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);
// 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
year2 = year;
month2 = month +1;
day2 = day;
//HERE I WANT TO ADD THE VARIABLES year2,month2,day2 AFTER BEING SET BY THE PREVIOUD METHOD IN THE LIST VIEW
}
}
public void showDatePickerDialog() {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(Somministrazione.this.getFragmentManager(), "datePicker");
}
}
I apologize for my language, English isn't my main course. Any kind of help would be appreciated ( i'm a newbie tho )
For the database part i have written in a file called Database.java this method:
public void InserisciSomministrazione(String data, String quantita, Integer Id_Farmaco){
ContentValues cv=new ContentValues();
cv.put(Somministrazione.DATA, data);
cv.put(Somministrazione.QUANTITA, quantita);
cv.put(Somministrazione.ID_FARMACO, Id_Farmaco);
DB.insert(Somministrazione.TABELLA_SOMMINISTRAZIONE, null, cv);
}