3

I'm new to Android and have been trying to use Parse.com. The code snippets provided on their website have been very useful. I've been able to send the data from my app to database, which means that it is connecting to the right database. But I'm unable to retrieve the correct data from it. No matter what object ID I use, it always returns either null or 0 (depending on if its a string or integer). My code is as follows :

public class MainActivity extends ActionBarActivity {



    TextView tv;
    Button b;
    String playerName;
    ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
    //int score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById (R.id.tv);
        b=(Button) findViewById (R.id.button1);
        Parse.initialize(this, "n12BWwZwk*************3dyKnuIUU", "vVi4TwI***************4peRD1VwyJ9CHw");
        final ParseObject gameScore = new ParseObject("GameScore");
       /* gameScore.put("score", 900);
        gameScore.put("playerName", "Pranav");
        gameScore.put("cheatMode", false);
        gameScore.saveInBackground();
      */


        query.whereEqualTo("objectId", "PnfRaUtHjB");
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> arg0, ParseException arg1) {
                // TODO Auto-generated method stub
                if (arg1==null)
                {
                    int score=gameScore.getInt("score");
                    tv.setText("Player score - "+score);
                    Log.d("PTesting", "Player Score");
                    //Toast.makeText(getApplicationContext(), "Reached till Toast "+score,
                        //     Toast.LENGTH_LONG).show();

                }
                else
                {
                    Log.d("score", "Error: " + arg1.getMessage());
                }
            }

        });

}
}

I've tried using the [ query.whereEqualto ] with playerName and score too. Same results.

I've also tried this:

ParseQuery<ParseObject> query=ParseQuery.getQuery("GameScore");
        query.getInBackground("YsHLfJ7tUB", new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject arg0, ParseException arg1) {
                // TODO Auto-generated method stub

                if (arg1==null)
                {
                    int score = gameScore.getInt("score");
                    playerName = gameScore.getString("playerName");
                    boolean cheatMode = gameScore.getBoolean("cheatMode");
                    tv.setText("Updating "+playerName);

                }
                else
                {
                    Log.d("score", "Error: " + arg1.getMessage());
                }
            }

The values returned are either 0 or null.

The database looks like this:

https://i.sstatic.net/0wv2x.jpg

1 Answer 1

2

you are using wrong object for retrieving. try this.

ParseQuery<ParseObject> query=ParseQuery.getQuery("GameScore");
        query.getInBackground("YsHLfJ7tUB", new GetCallback<ParseObject>() {

            @Override
            public void done(ParseObject arg0, ParseException arg1) {
                // TODO Auto-generated method stub

                if (arg1==null)
                {
                    int score = arg0.getInt("score");
                    playerName = arg0.getString("playerName");
                    boolean cheatMode = arg0.getBoolean("cheatMode");
                    tv.setText("Updating "+playerName);

                }
                else
                {
                    Log.d("score", "Error: " + arg1.getMessage());
                }
            }

and i will suggest use subclasses for parse. http://blog.parse.com/2013/05/30/parse-on-android-just-got-classier/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Vivart! I didn't realize that! Guess i need to improve my basics! And thanks for the tip! I really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.