0
votes

I've been trying to add Parse to my android app. Everything is fine setting it up. Adding certain data to the cloud and users etc. I'm trying to add data from an classes ArrayList that sends out params. The Class Collection from the arraylist is fine which is called Tasks. It has the correct information. I set up a new class which extends the ParseObject, which is below, that should fill in for the Tasks class to enter the data to the Parse Cloud.

package beans;

import com.parse.ParseClassName;
import com.parse.ParseObject;
import com.parse.ParseUser;

import java.util.ArrayList;

/**
* Created by KieranMcc on 11/01/2016.
*/
@ParseClassName("Tasks")
public class ParseTasks extends ParseObject {
private int id; //_id
private int task_id; //task_id
private String task; //task_name
private boolean completed; //_isCompleted

public ParseTasks(){
    super();
}

public ParseTasks(Tasks tasks){
    super();
}


public int getId() {
    return getInt("_id");
}

public void setId(int id) {
    put("_id", id);
}

public int getTask_id() {
    return getInt("task_id");
}

public void setTask_id(int task_id) {
    put("task_id", task_id);
}

public String getTask() {
    return getString("task_name");
}

public void setTask(String task) {
    put("task_name", task);
}

public boolean isCompleted() {
    return getBoolean("_isCompleted");
}

public void setCompleted(boolean completed) {
    put("_isCompleted", completed);
}

public void setUser(ParseUser user){
    put("tasks_user", user);
}

public ParseUser getUser(){
    return getParseUser("task_user");
}
}

What I'm trying to do is loop through the arraylist with a collection of the class Tasks. ArrayList

This is my code but it isn't saving to Parse

protected void uploadToCloud(Task task){

    ParseTask taskParse = new ParseTask();
    taskParse.setUser(ParseUser.getCurrentUser());
    taskParse.setId(task.getId());
    taskParse.setName(task.getName());
    taskParse.setNumOfTasks(task.getNumOfTasks());
    taskParse.setNumOfTasksCompleted(task.getNumOfTasksCompleted());

    taskParse.saveInBackground();

    // add task to cloud
    // loop through tasks and add one by one to cloud
    ParseTasks tasksParse= new ParseTasks();
    for(int i = 0; i < task.getTasks().size(); i++){
        tasksParse.setId(task.getTasks().get(i).getId());
        tasksParse.setTask(task.getTasks().get(i).getTask());
        tasksParse.setTask_id(task.getTasks().get(i).getTaskId());
        tasksParse.setCompleted(false);
        tasksParse.setUser(ParseUser.getCurrentUser());
        tasksParse.saveInBackground();
    }

}

Not quite sure what i'm doing wrong as I don't get an error or anything. And the ParseTask about goes through fine? Can someone tell me why it wont go through please. Thank you very much for reading over the long post and for any help :)

1
Kmc u want to store multiple data into parse class? and also get call back of the save in background thread for what u getting error. - Hardik Parmar
Hey Hardik! I managed to get it working in the end :)! What unwanted to do was just upload a list of classes to the cloud. Needed to use the method saveAllInBackground a certain way and they all saved to it :). Thanks for checking up :) - SmiffyKmc

1 Answers

0
votes

I managed to find out what the problem was. So thought I'd share what I learned for anyone having the issue in the future :)

With parse you should add more then one piece of data in a series yourself. Like I tried at the top! Using the method saveAllInBackground() is what you should use.

What you need to have is a List of ParseObjects and pass them into saveAllInBackground(list, callbackmethod) like so. I then added a constructor with params and used that to save to the cloud by using the constructor as a new instance.

Hope it might help some people in the future :)