Есть некая модель даных
public class Crime {
private static final String JSON_ID = "id";
private static final String JSON_TITLE = "title";
private static final String JSON_SOLVED = "solved";
private static final String JSON_DATE = "date";
private UUID mId;
private Date mDate;
private boolean mSolved;
private String mTitle;
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
public boolean isSolved() {
return mSolved;
}
public void setSolved(boolean solved) {
mSolved = solved;
}
public Crime() {
mId = UUID.randomUUID();
mDate = new Date();
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public UUID getId() {
return mId;
}
@Override
public String toString() {
return mTitle;
}
}
Надо сделать добавление этих даных на парс. С этим справился
ParseObject object = new ParseObject("Crime");
object.put(Reminder.JSON_ID, reminder.getId()+"");
object.put(Reminder.JSON_TITLE, reminder.getTitle());
object.put(Reminder.JSON_DETAILS, reminder.getDetails());
object.put(Reminder.JSON_COMPLETED, reminder.isCompleted());
object.put(Reminder.JSON_DATE, reminder.getDate().getTimeInMillis());
object.put(Reminder.JSON_IMAGEPos, reminder.getImagePos());
object.put(Reminder.JSON_TOGLE_REMINDER, reminder.isOnOff());
object.put(Reminder.JSON_REQ, reminder.getReqCode());
object.put("user", ParseUser.getCurrentUser());
object.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.i("Save my reminders", "Reminder save(Parse)");
}
});
а вот как реализовать обновление и удаление, уже не могу понять есть некий сингелтон в который грузятся даные и используються в програме...
public class CrimeLab {
private static final String TAG = "CrimeLab";
private ArrayList<Crime> mCrimes;
private static CrimeLab sCrimeLab;
private Context mContext;
private ParseObject object = new ParseObject("Crime");
private CrimeLab(Context context){
mContext = context;
try {
Log.d("CrimeJSON", "download");
mCrimes = loadCrimes();
} catch (Exception e) {
mCrimes = new ArrayList<Crime>();
Log.e(TAG, "Error loading Crime: ", e);
}
isOnline();
}
public static CrimeLab get(Context c){
if(sCrimeLab == null){
sCrimeLab = new CrimeLab(c.getApplicationContext());
}
return sCrimeLab;
}
public ArrayList<Crime> getCrimes(){
return mCrimes;
}
public Crime getCrime(UUID id){
for(Crime c : mCrimes){
if(c.getId().equals(id))
return c;
}
return null;
}
public void addCrime(Crime c){
mCrimes.add(0, c);
}
public void deleteCrime(Crime c){
mCrimes.remove(c);
saveCrimes();
}
public boolean isOnline() {
String cs = Context.CONNECTIVITY_SERVICE;
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(cs);
Log.i("isOnline", "===" + cm.getActiveNetworkInfo());
if (cm.getActiveNetworkInfo() == null) {
return false;
}
Log.i("isOnline", "return " + cm.getActiveNetworkInfo().isConnectedOrConnecting());
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
public void loadCrimes(){...}
public void UpdateOrSaveCrimes(final Crime Crime) {
int n = 0;
//это работает не правильно
for(Crime c : mCrimes){
if(c.getId().equals(Crime.getId())){
updateCrime(Crime);
Log.i("Update", "Crime(new)");
n++;
}
}
if(n==1){
Log.i("SAve","Crime(new)");
saveCrime(Crime);
}else{
Log.i("ERROR","ERROR");
}
n=0;
}
public void saveCrime(Crime Crime){
object.put(Crime.JSON_ID, Crime.getId()+"");
object.put(Crime.JSON_TITLE, Crime.getTitle());
object.put(Crime.JSON_DETAILS, Crime.getDetails());
object.put(Crime.JSON_COMPLETED, Crime.isCompleted());
object.put(Crime.JSON_DATE, Crime.getDate().getTimeInMillis());
object.put("user", ParseUser.getCurrentUser());
object.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.i("Save my Crimes", "Crime save(Parse)");
}
});
}
public void updateCrime(final Crime Crime) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Crime");
query.whereContains(Crime.JSON_ID, Crime.getId().toString());
query.whereContains("user", ParseUser.getCurrentUser().toString());
query.getInBackground(object.getObjectId(), new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
object.put(Crime.JSON_ID, Crime.getId() + "");
object.put(Crime.JSON_TITLE, Crime.getTitle());
object.put(Crime.JSON_DATE, Crime.getDate().getTimeInMillis());
object.put(Crime.JSON_COMPLETED, Crime.isCompleted());
object.put("user", ParseUser.getCurrentUser());
object.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.i("Save my Crimes", "Crime save(Parse)");
}
});
}
}
});
}
}
Функцию сохранение или обмена использую в onPause ( и эта функция(сохранение или обмена) мне открыв 2\3 раза так и должно быть по скок там viewPager и он прогружает ближайшие елементы...
Не могу ни как реализовать этот обмен...