chavi minou
chavi minou

Reputation: 13

save the button was click and display the option with date as summary in android app

choose which diaper is chosen and record the date and time, every time a diaper is changed then save it.

enter image description here

I tried to use shared preference but i do not really know how to use it properly as I am completely new to this.

Upvotes: 1

Views: 62

Answers (2)

Saneesh
Saneesh

Reputation: 1864

Use Realm to store data in database

To store data is like

Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
DateModel datemodel = realm.createObject(DateM.class);
datemodel.setDate(your date);
realm.commitTransaction();

Get stored data :

RealmResults< DateModel > result = realm.where(DateModel.class)
                .findAll();

Upvotes: 0

RajatN
RajatN

Reputation: 223

Try This Define an Array list in which you will save your date time

SharedPreferences  sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
List<Date> dateList = new ArrayList<>();

Then on diaper change function try this

public void onDiaperChange(Date date){

if(sharedpreferences.contains("DateList")){
     dateList = sharedpreferences.getString("DateList","");
}
dateList.add(date);

sharedpreferences.putString(ObjectSerializer.serialize(dateList));

}

and to get the list use this

public void getAllDateTime(){
List<Date> fetchedData = new ArrayList<>();
 fetchedData = (ArrayList<Date>) ObjectSerializer.deserialize(prefs.getString("DateList", ObjectSerializer.serialize(new ArrayList<Date>())));
  } catch (IOException e) {
}

you can get ObjectSerializer from here ObjectSerializer

Upvotes: 1

Related Questions