I am trying to save and retrieve some object data on parse local datastore. My problem is that say I have saved two objects in this case LocalPrinterDetail
objects, the save and retrieval of the data work fine and data is displayed correctly on my recyclerview, however, when I exit the app and open it up again the printerName
and the stationName
are null for each of the two objects but the localPrinterDetailObjects
shows that it has a size of 2 during debug thus on my recyclerview blank items are shown. Why is this happening on app exit and why can't I cache the data?
Retrieve method
public void retrieveLocalPrinterDetailRecords() {
ParseQuery<LocalPrinterDetail> query = ParseQuery.getQuery(LocalPrinterDetail.class);
query.fromLocalDatastore();
query.findInBackground(new FindCallback<LocalPrinterDetail>() {
@Override
public void done(List<LocalPrinterDetail> localPrinterDetailObjects, ParseException e) {
if (e == null) {
if (localPrinterDetailObjects != null && !localPrinterDetailObjects.isEmpty() && localPrinterDetailObjects.get(0) != null) {
localPrinterList.clear();
localPrinterList.addAll(localPrinterDetailObjects);
}
setupRecyclerView((RecyclerView) recyclerView);
adapter.notifyDataSetChanged();
} else {
e.printStackTrace();
}
}
});
}
Save method
public void saveLocalPrinterDetailRecord(String printerStationName) {
LocalPrinterDetail localPrinterDetail = new LocalPrinterDetail();
localPrinterDetail.setStationName(printerStationName);
localPrinterDetail.setPrinterName(printerModelName);
localPrinterDetail.put("station_name", localPrinterDetail.getStationName());
localPrinterDetail.put("printer_name", localPrinterDetail.getPrinterName());
localPrinterDetail.pinInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(context, "Local Printer Saved", Toast.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});
localPrinterDetail.saveEventually();
}
LocalPrinterDetail class
@ParseClassName("LocalPrinterDetail")
public class LocalPrinterDetail extends ParseObject {
private int printerId;
private String printerName;
private String stationName;
public LocalPrinterDetail() {
}
public int getPrinterId() {
return printerId;
}
public void setPrinterId(int printerId) {
this.printerId = printerId;
}
public String getPrinterName() {
return printerName;
}
public void setPrinterName(String printerName) {
this.printerName = printerName;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
}
Try to change your subclass declaration to:
@ParseClassName("LocalPrinterDetail")
public class LocalPrinterDetail extends ParseObject {
public int getPrinterId() {
return getInt("printerId");
}
public void setPrinterId(int printerId) {
put("printerId", printerId);
}
public String getPrinterName() {
return getString("printerName");
}
public void setPrinterName(String printerName) {
put("printerName", printerName);
}
public String getStationName() {
return getString("stationName");
}
public void setStationName(String stationName) {
put("stationName", stationName);
}
}