N
N
Nick20152016-01-22 02:06:04
Android
Nick2015, 2016-01-22 02:06:04

How to make a data model correctly?

There is a data model

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;
    }
}

It is necessary to add these data to the pars. Dealt with it
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)");
            }
        });

but I can’t understand how to implement updating and deleting, there is a certain singelton into which data is loaded and used in the program ...
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)");
                        }
                    });
                }
            }
        });
    }
}

I use the save or exchange function in onPause ( and this function (save or exchange) opened 2/3 times for me, so it should be there for the viewPager and it loads the next elements ...
I can’t implement this exchange ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick2015, 2016-01-22
@Nick2015

up?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question