parse.com update data android - android

I have an object at parse.com in the class Grillen with data.
Now I want to make it such that if I click a button it adds to the row with the same name as selected in a spinner, and it updates the columns Betrag and Rechnung. I tried the following code but it doesn't work.
public void createRechnung(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen")
// Retrieve the object by id
query.getInBackground(String.valueOf(mNameInput), new GetCallback<ParseObject>() {
public void done(ParseObject Grillen, ParseException e) {
if (e == null) {
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground();
}
}
});
}

Use this code to check what's going wrong
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
// Retrieve the object by id
query.getInBackground(String.valueOf(mNameInput), new GetCallback<ParseObject>() {
public void done(ParseObject Grillen, ParseException eg) {
if (eg == null) {
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
If you successfully save, then your exception is null, if it didn't you will know why via stacktrace. I suggest running this code and letting the execution flow of the app be based on whether or not the save was successful or not.
Read this: getInBackground(String, callback) You must pass the objectId of the object you are trying to get, rather than the name. Make sure you are getting by ObjectID and not name as it appears you are doing. ObjectId looks like a random string and should be found in your database by looking online on Parse
Do this instead:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
query.whereEqualTo("name", String.valueOf(mNameInput));
// Retrieve the object by id
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException eg) {
if (eg == null && objects.size() > 0) {
ParseObject Grillen = objects.get(0);
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
Note: I have no idea what the column name is for the 'name' attribute of Grillen objects, so you might need to change this to whatever you call the name column for that object.

Okay i have get the Spinner to work for the name but not the EditText
here is some part of the codes
public class RechnungActivity extends AppCompatActivity {
private EditText mBetragInput;
private Spinner mNameInput;
private String mName;
private String mBetrag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechnung);
ParseObject.registerSubclass(Grillen.class);
mBetragInput = (EditText) findViewById(R.id.Sonstiges);
mNameInput = (Spinner) findViewById(R.id.Name);
mName = mNameInput.getSelectedItem().toString();
mBetrag = mBetragInput.getText().toString();
}
public void createRechnung(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
query.whereEqualTo("name", mName);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException eg) {
if (eg == null && objects.size() > 0) {
mBetrag = mBetragInput.getText().toString();
ParseObject Grillen = objects.get(0);
Grillen.put("Betrag", mBetrag);
Grillen.put("Rechnung", true);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
}

Related

Parse Frame Work Update Query does not work

I am using parse frame work in android, I am updating user object in that but I update that old data is replaced with new data here my code is . How to resolve it any one help me solve out it.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", currentUser.getObjectId());
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
currentUser.put("forbiddenWords", wordArray);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
hideProgressDialog();
if (e == null) {
Snackbar.make(parentView, R.string.string_setting_chnaged, Snackbar.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});
}
});
I guess you wanna update some value of your current user. Try this code
ParseUser user = ParseUser.getCurrentUser();
user.put("forbiddenWords", wordArray);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
hideProgressDialog();
if (e == null) {
Snackbar.make(parentView, R.string.string_setting_chnaged, Snackbar.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});

Parse.com cannot get ParseRelation from Object

i try to retrieve relation from an object. but i get an error (This query has an outstanding network connection. You have to wait until it's done.).
I tried everything, but i get this error.
public void Member() {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"PrivateDialog");
try {
mFriendsRelation = query.get(relationid).getRelation("Member");
} catch (ParseException e1) {
e1.printStackTrace();
}
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
mFriends = friends;
if (member.getAdapter() == null) {
member.setAdapter(new MemberAdapter(member.getContext(), mFriends, R.layout.member_group_item));
member.setHasFixedSize(true);
member.setLayoutManager(new LinearLayoutManager(GroupDialogActivity.this));
member.setItemAnimator(new DefaultItemAnimator());
} else {
// refill the adapter!
Log.i("Refill", "todo");
}
} else {
Log.e("TAG", e.getMessage());
}
}
});
}
});
}

Android Parse.com Unable to remove an item from Array field in both installation and User object

I've two array fields in my parse app one in Installation object named as "subscriber" and other in User object named as "SubsUser".
I've been trying to remove an item from both arrays by using this code...
user.getList("SubsUser").remove(item);
user.save();
installation.getList("subscriber").remove(item);
installation.save();
but even after using this code the items are visible on parse dashboard, it seems both my tables are not being updated.
You should use put method to let ParseObject aware data is dirty.
ParseUser user = ParseUser.getCurrentUser();
List<Object> list = user.getList("SubsUser");
list.remove(item);
user.put("SubsUser", list);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d(TAG, "saved in background");
if (e == null)
//ok
else
//something else went wrong
}
});
I myself solved the problem, i was using incorrect code
the write code to remove an object or collection of object from a parse array is
List<String> list=new ArrayList<String>();
list.add(item);
user.removeAll("SubsUser",list);
user.save();
installation.removeAll("subscriber",list);
installation.save();
I have a solution too, with a dialog
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.dialogo_si_no);
dialog.setTitle("La Lista");
Button btnAceptar = dialog.findViewById(R.id.btnSi);
Button btnCancelar = dialog.findViewById(R.id.btnNo);
TextView lblMensaje = dialog.findViewById(R.id.lblMensaje);
lblMensaje.setText("¿Deseas eliminar este evento de tus favoritos?");
dialog.show();
btnAceptar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RelativeLayout layout = (RelativeLayout) view.getParent();
TextView lblObjectId1 = (TextView)layout.getChildAt(0);
final String objectId = lblObjectId1.getText().toString();
final ParseQuery<ParseObject> qFavoritos = ParseQuery.getQuery("favoritos").whereEqualTo("usuarioId",sharedpreferences.getString("globalUserId","0"));
try {
List<ParseObject> resultado = qFavoritos.find();
final String id = resultado.get(0).getObjectId();
qFavoritos.getInBackground(id, new GetCallback<ParseObject>() {
#Override
public void done(final ParseObject object, ParseException e) {
if (e == null) {
List<Object> lista = object.getList("eventos");
lista.remove(objectId);
object.put("eventos",lista);
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d(TAG, "saved in background");
if (e == null) {
Log.d(TAG, "favorito eliminado");
Intent intent = new Intent(activity, EventosActivity.class);
activity.startActivity(intent);
}else{
Log.d(TAG, e.getMessage());
}
}
});
}else{
Log.d(TAG,e.getMessage());
}
}
});
}catch (Exception ex){
Log.d(TAG,ex.getMessage());
}
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});

Retrieve data from parse based on object ID?

I have a list of objectId where I am trying to retrieve another list in another class where the pointer value equals the objectId of the current parseObject.
I realised that I am comparing a string "objectId" to a pointer "threadPointer" so can someone help me with the query to retrieve a list of items from a class if the objectId equals the threadPointer _Pointer value
public void retrieveThreadListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.include("postedBy");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
// TODO : Notify to refresh data
}
} else {
Log.d("DEBUG", "ERROR PINNING DATA WITH EXCEPTION : " + e);
}
}
});
}
}
});
}
public void retrievePostsListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.include("postedBy");
query.include("threadPointer");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> list, ParseException e) {
if (e == null) {
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
// TODO : Notify to refresh data
Log.d("DEBUG: ","Finished retrieval");
Log.d("DEBUG", list.toString());
ParseObject object = (ParseObject)list.get(1).get("threadPointer");
Log.d("DEBUG_THREAD_POINTER", object.getObjectId());
}
} else {
Log.d("DEBUG", "ERROR PINNING DATA WITH EXCEPTION : " + e);
}
}
});
}
}
});
}
This is where I am retrieving the ThreadList from local db
public void retrieveThreadList(){
List<ParseObject> parseObjectList = new ArrayList<ParseObject>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.orderByDescending("updatedAt");
query.fromLocalDatastore();
// Data retrieval was successfull
try {
parseObjectList= query.find();
} catch (ParseException e) {
e.printStackTrace();
}
for (ParseObject parseObject : parseObjectList) {
String title = (String) parseObject.get("threadTitle");
ParseUser parseUser = (ParseUser) parseObject.get("postedBy");
String profilePictureUrl="local";
try {
profilePictureUrl = ((ParseFile) parseUser.get("profilePicture")).getUrl();
}catch (Exception exception)
{
Log.d("DEBUG_FB","profilePicture not retrieved");
}
String threadObjectId = parseObject.getObjectId();
Log.d("DEBUG_FB_THREAD_ID",threadObjectId);
ParseQuery parseQueryToRetrievePosts = ParseQuery.getQuery("Posts");
parseQueryToRetrievePosts.fromLocalDatastore();
//----****** NEED HELP HERE ******----
parseQueryToRetrievePosts.whereEqualTo("threadPointer",parseObject.getObjectId());
try {
List<ParseObject> postsList = parseQueryToRetrievePosts.find();
Log.d("DEBUG_FB_POSTS_LIST",postsList.toString());
} catch (ParseException e) {
e.printStackTrace();
}
/* To retrieve data over the network --- THIS WORKS WELL BUT I DONT WANT A NETWORK CALL
ParseRelation <ParseObject> posts = parseObject.getRelation("postMessage");
ParseQuery postsQuery = posts.getQuery();
List<ParseObject> postsList = new ArrayList();
try {
postsList=postsQuery.find();
for (ParseObject parseObject1: postsList)
{
String postMessage = (String)parseObject1.get("postMessage");
Log.d("DEBUG_FB_MSG", postMessage);
}
Log.d("DEBUG_FB_POSTS", postsList.toString());
}catch (Exception e)
{
}
*/
threadModelStore threadObject = new threadModelStore(title,profilePictureUrl);
Log.d("DEBUG_FB", profilePictureUrl);
Log.d("DEBUG_FB", title);
threadItemList.add(threadObject);
}
Sir my scenario is I have these classes
THREADS(Class) which has POSTS(RELATION TO _POSTS) class I want to retrieve the related posts of a particular thread. I was unable to pin
relations to local data store please let me know if there is a way to
do so ? -- (UNSOLVED)
So what I want to do is to compare the thread pointer in the POSTS
class and retrieve the related posts. For this I needed to compare a
string object id to pointer field _ThreadPointer in POSTS Class
Suggest a possible solution -- SOLVED BY ( VED ).
ParseObject threadPointer=ParseObject.createWithoutData("Pointer_class_Name", "pointer_object_id");
ParseQuery<ParseObject> query=ParseQuery.getQuery("className");
query.whereEqualTo("_Pointer", threadPointer);
query.include("_Pointer");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> listCommunity, ParseException e) {
// TODO Auto-generated method stub
Log.d(TAG, "done");
if(e==null){
// access your data
}else{
// Error in retrieving data
}

Android Parse Login with email or username

I'm trying to make an android app using parse that allows one to login using either their username or email. I have tried querying to get an object with a provided email address, then get that object's username to login with. However, I am getting this error:
'getFirstInBackground(com.parse.GetCallback)' in 'com.parse.ParseQuery' cannot be applied to '(anonymous com.parse.GetCallback "com.parse.ParseObject>)'
I'm new to app dev and parse, so I'm not entirely sure what this means. From what I understand it wont let me use getFirstInBackground because that works with parseUser's and I'm working with ParseObjects, but the code I'm using I pulled from a prior stackoverflow question where this was a working answer: Login username AND email in Parse Android
Here is my code:
// Do this to allow for username or email log in
if (mEmail.indexOf("#") != -1) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("email", mEmail);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
String actualUsername = object.getString("username");
ParseUser.logInInBackground(actualUsername, mPassword, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (e != null) {
// TODO: Show error message
Toast.makeText(LoginActivity.this, "Credentials incorrect", Toast.LENGTH_LONG).show();
} else {
// Start Intent for activity
// TODO: Choose activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
}
}
});
According to docs, the method's signature is as follows:
class com.parse.ParseQuery<T> {
public void getFirstInBackground(GetCallback<T> callback)
}
That means that the generic type of GetCallback must be the same as in your ParseQuery. So it probably should be
query.getFirstInBackground(new GetCallback<ParseUser>()) { /* ... */ }
You are attempting to query a ParseUser with a GetCallBack of type ParseObject.
Replace all instances of ParseObject with ParseUser in your code.
query.getFirstInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
String actualUsername = user.getUsername();
ParseUser.logInInBackground(actualUsername, mPassword, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (e != null) {
// TODO: Show error message
Toast.makeText(LoginActivity.this, "Credentials incorrect", Toast.LENGTH_LONG).show();
} else {
// Start Intent for activity
// TODO: Choose activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
}
}
});
Hope this helps
final String userOrEmal = editTextEmailOrUserName.getText().toString().trim();
final String mpassword = editTextPassword.getText().toString().trim();
if (userOrEmal.contains("#")) {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("email", userOrEmal);
query.getFirstInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser object, ParseException e) {
if (object != null) {
ParseUser.logInInBackground(object.getString("username"), mpassword, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.v("login", "secessful");
} else {
Log.v("login", "fail");
}
}
});
} else {
}
}
});
} else {
ParseUser.logInInBackground(userOrEmal,mpassword, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.v("login", "secessful");
} else {
Log.v("login", "fail");
}
}
});
}

Categories

Resources