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 :)
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 :)
Related
I am working on a shopping car app and need to have the product list and every product stock. for this what I am trying to do is implement a global list variable. I have read about global variables and I could implement it, but can't figure out how a list would be. any suggestions or help on how to implement it would be great, thanks.
public class Application extends android.app.Application {
private int data;
public int getData(){
return data;
}
public void setData(int d){
this.data=d;
}
}
((Application) this.getApplication()).setData(0);
x1=((Application) this.getApplication()).getData();
I figure it out following your suggestions. first on the applicacion class
public class Application extends android.app.Application {
public ArrayList myGlobalArray = null;
public Application() {
myGlobalArray = new ArrayList();
}
}
then for the porpouse of my app I add the respective values on their respectives indexes
for (int i=0;i<productsList.size();i++){
if (productsList.get(i).idProduct==idProduct){
values.add(i,Integer.parseInt(txtQuantity.getText().toString()));
((Application)getApplicationContext()).myGlobalArray = (ArrayList) values;
}
}
and when I need to get the values
ArrayList<Integer> test = new ArrayList<>();
for (int d=0;d< ((Application) getApplicationContext()).myGlobalArray.size();d++) {
test.add((Integer) ((Application)getApplicationContext()).myGlobalArray.get(d));
}
Thank you!
I have the following Object and i want to use RxJava in order to create a new object. The logic behind this is that each article has a lot of comments. And it finds the correct comments using the ArticleData.commentId and the Comment.id.
public class ArticlesResponse {
public List<ArticleData> articles;
public List<Data> comments;
}
public class Data {
public int id;
public String link;
public String title;
public String author
public String body;
}
public class ArticleData extends Data {
public List<int> commentId;
}
So how can i use Rxjava in order to create the following object
public class Article extends Data {
public List<Comments> comments;
}
public class Comments extends Data {
// comments will have more attributes in the feature
// so use a seperate object
}
I know that i have to use the flapMap and the filter in order to parse the "ArticleResponse" but i don't know how to put all this together.
Furthermore the "ArticleResponse" is being generated from a json which i got from Retrofit, so i guess it will be better to use RxJava since i already have the Observable instead of putting nested for's inside my Callback.
I assume you means that articlesResponse.comments is a list contains all Comments of these all ArticleData, although I don't think wrap these data together and do the map operation in client is a good idea, this job should be done at server.
And I think maybe your ArticlesResponse's comments field should be a List<Comments>.
With these assumption, the code below may do the job you want (I put them in a TempTest class, and define an interface you described, and mock it to pass javac compile, and I also use Java 8 lambda grammar for code simplicity).
public class TempTest {
public static class Data {
public int id;
public String link;
public String title;
public String author;
public String body;
}
public static class ArticleData extends Data {
public List<Integer> commentId;
}
public static class Comments extends Data {
// comments will have more attributes in the feature
// so use a seperate object
}
public static class ArticlesResponse {
public List<ArticleData> articles;
public List<Comments> comments;
}
public class Article extends Data {
public List<Comments> comments;
}
public interface TestInterface {
Observable<ArticlesResponse> getArticle();
}
public static Comments findCommentWithId(int commentId, List<Comments> comments) {
for (Comments comment : comments) {
if (comment.id == commentId) {
return comment;
}
}
return null;
}
#Test
public void simpleTestcase() {
// assume you means that articlesResponse.comments is a list contains all Comments of these
// all ArticleData, although I don't think wrap these data together and do the map operation
// in client is a good idea, this job should be done at server
TestInterface testInterface = mock(TestInterface.class);
testInterface.getArticle().map(articlesResponse -> {
List<Article> result = new ArrayList<>();
// for each ArticleData in articlesResponse.articles
for (ArticleData articleData : articlesResponse.articles) {
// get all Comments from articlesResponse.comments
Article article = new Article();
// ... copy Data field from articleData to article
article.comments = new ArrayList<>();
for (Integer id : articleData.commentId) {
Comments comment = findCommentWithId(id, articlesResponse.comments);
if (comment != null) {
article.comments.add(comment);
}
}
result.add(article);
}
return result;
}).subscribe(articles -> {
for (Article article : articles) {
System.out.println(article);
}
});
}
}
Kind of confused at what your actual question is, so hopefully this helps. Retrofit can return an Observable for you, which should make RxJava integration easy. For example, in your service you could make:
#GET(<your endpoint>)
Observable<ArticlesResponse> getArticles();
And call it like:
<yourService>.getArticles()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedules.mainThread())
.subscribe() // manipulate how you want
I'm kind of new to Android development.
I don't understand why the following code gives me a stackoverflowerror
Intent intent = new Intent(view.getContext(), MakeCall.class);
SipParcelable sipp = new SipParcelable(_sip);
intent.putExtra("sip", (Parcelable) sipp);
startActivity(intent);
Basically as soon as the startActivity(intent) fires, I get the following error:
I can get rid of the error by commenting out the third line with the putExtra() function.
I'm trying to pass my _sip object over to the MakeCall.class activity on another screen that's about to load up. I tried to follow the tutorial on how to implement a Parcelable class/object. Here's what my SipParcelable code looks like:
import com.myproject.library.SipService;
import android.os.Parcel;
import android.os.Parcelable;
public class SipParcelable implements Parcelable{
public SipService mData;
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeValue(mData);
}
public SipParcelable(SipService sip)
{
mData = sip;
}
// Parcelling part
public SipParcelable(Parcel in){
mData = (SipService) in.readValue(SipService.class.getClassLoader());
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public SipParcelable createFromParcel(Parcel in) {
return new SipParcelable(in);
}
public SipParcelable[] newArray(int size) {
return new SipParcelable[size];
}
};
}
What am I doing wrong?
Your SipService class must implement parcelabe and modify how SipService object is read and written from/to pracel.
check this tutorial it might help you
http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
You can use serialisable too… But parcelable is faster and better
NOTE: all properties of an object (if the properties are objects) that implements parcelable, must also be parcelable as well.
I've got a ListActivity with about 100 events. (These events are also displayed on a map in another activity.)
So I've my MyListActivity, which handles the list and a MyListAdapter to populate the list that deals with MyEvent-Objects. As model I have a MyEvent-model-Class and a MyEventStorage-Class.
Now have written a method to return an image for an event based on its ID. It does some decisions which image to load, where it gets the image from, loads it and resamples it.
Where should I put this method in best practice?
I don't want to copy it in every activity where it is needed but just in one place.
I'd like to have it in my MyEvent-Class, so I can call myEvent.getImage(); but it somehow feels wrong to put this method inside the model class with all the getters and setters. Is it wrong?
Should I write a helper class containing this method? As a static method? Would this still provide a good performance?
Or maybe create an additional MyImageGetter-object for every MyEvent-object?
Or expand the MyEvent-model with an image-variable and getters/setter and create an extra class that puts the proper image in the model? How would I call that method?
Another solution?
MyEvent.java:
public class MyEvent {
private int id;
private int category;
private String eventname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// other getters and setters
}
MyEventStorage.java:
private static MyEventStorage instance = null;
private List<MyEvent> store;
private MyEventStorage() {
store = new ArrayList<MyEvent>();
}
// get the storage containing the events
public static MyEventStorage getInstance() {
if (instance == null) {
instance = new MyEventStorage();
}
return instance;
}
public List<MyEvent> getStore() {
return store;
}
public void setStore(List<MyEvent> store) {
this.store = store;
}
// Add a Event to the store
public void addEvent(MyEvent myEvent) {
store.add(myEvent);
}
// Remove a Event from the store
public void removeEvent(MyEvent myEvent) {
store.remove(myEvent);
}
}
The method I want to integrate:
Image getImageById(int id) {
// decide which image to load based on the events id
// decide where to load the image from
// check if image available
// load image if available else load placeholder image
// resample image
return image;
}
Thank you advance!
I think your last bullet point is spot on.
If the Image is in fact a property of MyEvent, it makes sense to add an instance variable to that class. You shouldn't include the logic for retrieving an event's image from a datasource in the model, but rather use a static utility method to load this property.
Your getImageById method looks like it has to do a decent amount of work to retrieve the image from wherever it is stored. I think it would make the most sense to create a utility class (along the lines of ImageRetriever) like you mentioned in order to perform the actual retrieval of the image. This prevents you from having to copy the method to multiple places. Performance should not be a concern either, as you'll never have to instantiate this class.
The code could look something like this:
public class MyEvent {
private int id;
private int category;
private String eventname;
private Image image;
public MyEvent(int id...) {
// initialize instance vars
setImageFromRetriever();
}
public void setImage(Image image) {
this.image = image;
}
public void setImageFromRetriever() {
// optional null check if you don't want to reload images
setImage(ImageRetriever.getImageById(this.id));
}
}
I'm working on an android application using robospice, spring and jackson. Everything works fine except that cache results don't have relations of POJO classes.
For instance;
public class User extends SampleBase {
public int id;
public String name;
public Address address;
}
public class Address extends SampleBase {
public int id;
public String street;
public String city;
}
public class SampleBase {
// Base class of all POJO classes
}
When I send a request to get a user, I get all the values properly. However, when I try to get a user from cache, the address field returns null. If there's a relation between two classes, those relation fields are null but other fields are OK.
The result of request:
User:
id: 1
name: "Test User"
address: Address Object
The result of cache:
User:
id: 1
name: "Test User"
address: null
In my design, all of the POJO classes extend SampleBase. And there is only one RequestListener:
public class SampleRequestListener implements RequestListener<SampleBase> {
#Override
public void onRequestSuccess(SampleBase result) {
// Some operations
}
#Override
public void onRequestFailure(SpiceException e) {
// Some operations
}
}
I don't know if it's about my request listener but this works fine while sending requests. Is there something else that I should do to get the related objects of a cached object?
It is strange, we often do this and it works fine. Try to find some solution on jackson forum, RS is just a wrapper here.