繁体   English   中英

您如何在Android的两个不同活动之间通过Parse查询?

[英]How do you query from Parse between two different activities in Android?

因此我在一个活动中使用解析存储了一些基本数据,但是如何在另一个活动中从解析(查询)中检索该数据呢? 有人可以给我一个清晰的例子吗?

所以在我的主要活动中

 public String max = "max";
 Parse.enableLocalDatastore(this);
 private ParseObject rightCardsStore = new ParseObject("RightCardsStore");
 rightCardsStore.put("max",max);
 rightCardsStore.saveInBackground();

现在,在另一个活动“ Folder.java”中,我想查询/检索该数据并使用该字符串。

正如hitch.united所说,您需要通过执行saveInBackground来获取ID并将其发送到其他活动:

rightCardsStore.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException e) {
         if (e == null) { 
             Intent intent = new Intent(YourCurrentActivity.this, YourNewActivity.class);
             intent.putExtra("parseObjectId", rightCardsStore.getObjectId());
             YourCurrentActivity.this.startActivity(intent);
         }       
    }
});

然后,您可以检索其他活动中的数据,并查询解析:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    String id = bundle.getString("parseObjectId");

    ParseQuery<ParseObject> query = ParseQuery.getQuery("RightCardsStore");
     query.getInBackground(id, new GetCallback<ParseObject>() {
         public void done(ParseObject object, ParseException e) {
              if (e == null) { 
                 // object is the RightCardsStore you just saved
                 String max = object.getString("max");
              }
         }    
     }
}

如果只需要将max用作只读值,则可以通过替换(在第一个活动中)来简化过程

intent.putExtra("parseObjectId", rightCardsStore.getObjectId());

通过

intent.putExtra("max", max);

并通过以下方式替换第二项活动:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    String max = bundle.getString("max");
}

在saveInBackground之后,获取对象的ID并将其意图传递给新活动。 然后,您可以使用该ID重新查询以检索对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM