0

I have a Parse Dashboard account, In which I have a class called PointsCollected. For that class i have fields as device,pointsObtained.

My Question is I need to retrieve the points based on the device. I am getting the device id from telephone manager. But how to make a query to retrieve the points based on device id, And if no rows found it show add a row with zero points in the database.

  • Where are you storing the device id? – letsCode Aug 01 '17 at 17:34
  • i am storing the device id in device column in my PointsCollected table. – Janarthan Thumburu Aug 01 '17 at 17:35
  • You should separate that out. I am assuming you are using Mongo for it? The user joins, or however you handle it, get their device ID, and create a new table -> Device. In there , store the currentUser ID, as well as the device ID. then you can get the device id and store it in a string. then query the PointsCollected table, looking where you find the user, plus the object id and then get the points. – letsCode Aug 01 '17 at 17:36
  • my PointsCollected table has columns, "device", "pointsObtained" – Janarthan Thumburu Aug 01 '17 at 17:37
  • if you want to keep that, have your PointsCollected table ALSO collect the current user ID as well....and then you query it – letsCode Aug 01 '17 at 17:38

1 Answers1

1

You can do this if you put the user in the same table.

ParseQuery<ParseObject> query = ParseQuery.getQuery("PointsCollected");
    query.whereEqualTo("currentUser", currentUset.getObjectId());
    // this will find the user.
    // then find the first instance
    query.findFirstInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> object, ParseException e) {
            if (e == null) {
                textView.setText(object.getString("device"));
            } else {
                Log.d(TAG, "Error: " + e.getMessage());
            }
        }
    });

This is the new idea.... This should work.

    // At the end we want to get the points.
    final int points; 

    //First, lets get the ID of the device and store that into a veriable.
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    final String deviceId = telephonyManager.getDeviceId().toString();

    //Then lets query the PointsCollected database.
    ParseQuery<ParseObject> query = ParseQuery.getQuery("PointsCollected");
    // Then we want to see where the DeviceID (from the variable) matches the device from the database.
    // "device" is the row in the database.
    // deviceID is the varible that is storing that users device ID.
    query.whereEqualTo("device", deviceId);
    //Then we get the first instance...
    query.getFirstInBackground(new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
             // you want to do a check on the ParseException here as well. 
            if (object == null) {
                Log.d("nothing found", "let's go ahead and create a new object.");
                //ADD THE OBJECT AS A NEW OBJECT!!!!
            } else {

                points = object.getInt("pointsObtained");
                Log.d("points found", points + "");
            }
        }
    });
letsCode
  • 2,774
  • 1
  • 13
  • 37
  • The problem is i am not having any user details. I am storing points only based on device id. So in my case "currentUser" is not present. Also i need to get points not the device id. how the code will be like??. I need to get the points when i search for stored device id. If device id is not found in the table. then it should save the device id and points with ´0´ as default. Thanks for help. – Janarthan Thumburu Aug 01 '17 at 21:33
  • How did you get the device I'd? Do it again before your query. So, you can get the device I'd? Get the device I'd and store it as a string. And inside on whereEqualTo do "device", deviceID – letsCode Aug 01 '17 at 23:16
  • Then where I have object.getString do object.getInt with the name of that row in there instead of device – letsCode Aug 01 '17 at 23:17
  • How new are you to parse? Because this is a basic query – letsCode Aug 01 '17 at 23:19
  • ParseQuery query = ParseQuery.getQuery("PointsCollected"); query.getInBackground("HKPeQDVn5L", new GetCallback() { public void done(ParseObject object, ParseException e) { if (e == null) { mPoints=object.getInt("pointsObtained"); } else { TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);String deviceId=telephonyManager.getDeviceId().toString(); mPoints=0; object=new ParseObject("PointsCollected"); object.put("pointsObtained",mPoints); objects.put("device",deviceId); } } }); – Janarthan Thumburu Aug 02 '17 at 15:10
  • I tried yesterday by giving that code. It gave me the points which are stored in table. but i need to get the points without giving that objectid "HKPeQDVn5L". Since i can´t give the object id directly there as it will differ from devices. i only know the device id. so in order to get the points i need to query based on device id. so any solution – Janarthan Thumburu Aug 02 '17 at 15:13
  • whats the code you are using for the device id? does it return a string? – letsCode Aug 02 '17 at 15:15
  • Also i already tried giving device id in .whereEqualTo("device",deviceId). But it is giving me parse exception like null. – Janarthan Thumburu Aug 02 '17 at 15:16
  • take a snapshot of your database for me if you can.....black our any info, edit your question with the image. – letsCode Aug 02 '17 at 15:17
  • yes. i am using TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE‌​);String deviceId=telephonyManager.getDeviceId().toString(); and it is giving me the perfect device id (imei) and it is a string – Janarthan Thumburu Aug 02 '17 at 15:17
  • I don´t have reputations. So stackoverflow is not allowing me to upload image. but this is the thing availavle in my database. objectId(String): HKPeQDVn5L device(String): 911401503830521 pointsObtained(Number):3 – Janarthan Thumburu Aug 02 '17 at 15:38
  • Let me try that. Thank you. – Janarthan Thumburu Aug 02 '17 at 16:10
  • Hi. Thank you that code worked with little modification. when i give this code in the starting the application is crashing. TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); final String deviceId = telephonyManager.getDeviceId().toString(); So i gave this getting device where ever required and i got my points from the database. And also wrote the code to update the points when ever that device gets the points. Thank you for your help. – Janarthan Thumburu Aug 03 '17 at 15:07
  • hey, thats great news! Good job! Glad I could help you out. – letsCode Aug 03 '17 at 15:08