Ali Bdeir
Ali Bdeir

Reputation: 4365

How can I update an int on the Click of a button adding 1 to that int in the parse cloud?

Here's my code so far:

package com.alibdeir.database;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
import com.parse.ParseException;

public class MainActivity extends AppCompatActivity {
    TextView output;
    Button increase;
    int currentInt;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Parse.initialize(this, "QeaC0wiXsL9MyRbLaQPBWA9QzXim****lE7b8HjP", "kDjoZVRu4OyWOGxPtPjh7f*bXKFxp**VYO7g2qqd");
        output =  (TextView) findViewById(R.id.output);
        increase = (Button) findViewById(R.id.increase);

//        try {
//            Parse.initialize(this, "QeaC0wiXsL9MyRbLaQPBWA9QzXimS7vPlE7b8HjP", "QeaC0wiXsL9MyRbLaQPBWA9QzXimS7vPlE7b8HjP");
//            ParseQuery uptodatequery = new ParseQuery("refresh");
//            ParseObject refresh = uptodatequery.get("n6zA3CHxGx");
//            output.setText(refresh.toString());
//        } catch (com.parse.ParseException e1) {
//            e1.printStackTrace();
//        }

    }
    public void increase(View v) {

        currentInt = Integer.parseInt(output.getText().toString());
        output.setText(currentInt + 1 + "");
//        try {
//            Parse.initialize(this, "QeaC0wiXsL9MyRbLaQPBWA9QzXimS7vPlE7b8HjP", "QeaC0wiXsL9MyRbLaQPBWA9QzXimS7vPlE7b8HjP");
//            output =  (TextView) findViewById(R.id.output);
//            increase = (Button) findViewById(R.id.increase);
//            ParseQuery query = new ParseQuery("int");
//            ParseObject upload = query.get("n6zA3CHxGx");
//            upload.increment("n6zA3CHxGx",currentInt);
//            upload.saveInBackground();
//        } catch (com.parse.ParseException e1) {
//            e1.printStackTrace();
//        }
        ParseQuery<ParseObject> currentNumber = ParseQuery.getQuery("currentNumber");

// Retrieve the object by id
        currentNumber.getInBackground("n6zA3CHxGx", new GetCallback<ParseObject>() {
            public void done(ParseObject currentNumber, ParseException e) {
                if (e == null) {
                    // Now let's update it with some new data. In this case, only cheatMode and score
                    // will get sent to the Parse Cloud. playerName hasn't changed.
                    int currentInt = currentNumber.getInt("currentNumber");
                    currentNumber.put("currentNumber", currentInt + 1);
                    currentNumber.saveEventually();
                }
            }
        });
    }




//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_main, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks an the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }
//
//        return super.onOptionsItemSelected(item);
//    }
}

I tried so many stuff, including the ones I've commented.

public void increase

is my onclick method for a button. When you click that button, the textview output changes, the int, which is currentInt, gets a +1. What I want to do is onClick of the button I also want it to update the int in the database. How can I do that?

Upvotes: 0

Views: 41

Answers (1)

Mo Nazemi
Mo Nazemi

Reputation: 2717

You can perform an atomic increment provided by Parse.

currentNumber.increment("currentNumber");
currentNumber.saveInBackground();

Upvotes: 1

Related Questions