【问题标题】:how to retrieve specific row data from parse database for android如何从 android 的解析数据库中检索特定的行数据
【发布时间】:2017-08-01 17:09:50
【问题描述】:

我有一个 Parse Dashboard 帐户,其中有一个名为 PointsCollected 的类。对于那个类,我有字段作为设备,pointsObtained。

我的问题是我需要根据设备检索积分。我正在从电话管理器获取设备 ID。但是如何根据设备 id 进行查询以检索点,如果没有找到行,则在数据库中添加一个零点的行。

【问题讨论】:

  • 您将设备 ID 存储在哪里?
  • 我将设备 ID 存储在我的 PointsCollected 表的设备列中。
  • 你应该把它分开。我假设您正在使用 Mongo 吗?用户加入,或者您如何处理它,获取他们的设备 ID,并创建一个新表 -> 设备。在那里,存储当前用户 ID 以及设备 ID。然后您可以获取设备ID并将其存储在一个字符串中。然后查询 PointsCollected 表,查看您在哪里找到用户,加上对象 ID,然后获取积分。
  • 我的 PointsCollected 表有列“device”、“pointsObtained”
  • 如果你想保留它,让你的 PointsCollected 表也收集当前用户 ID....然后你查询它

标签: android parse-platform back4app


【解决方案1】:

如果您将用户放在同一个表中,您可以这样做。

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());
            }
        }
    });

这是新的想法.... 这应该可以。

    // 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 + "");
            }
        }
    });

【讨论】:

  • 问题是我没有任何用户详细信息。我仅根据设备 ID 存储积分。所以在我的情况下,“currentUser”不存在。我还需要获得积分而不是设备 ID。代码将如何?当我搜索存储的设备 ID 时,我需要获得积分。如果在表中未找到设备 ID。然后它应该将设备 ID 和点保存为默认值“0”。感谢您的帮助。
  • 你是如何得到我想要的设备的?在查询之前再做一次。那么,你能得到我想要的设备吗?获取我想要的设备并将其存储为字符串。并在 whereEqualTo 里面做“device”,deviceID
  • 然后在我有 object.getString 的地方用该行的名称而不是 device 执行 object.getInt
  • 你对解析有多大的了解?因为这是一个基本查询
  • ParseQuery 查询 = 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); } } });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
相关资源
最近更新 更多