简体   繁体   English

Android Parse Local Datastore返回Empty

[英]Android Parse Local Datastore Returns Empty

I have been working on an application where you input childs names and ages, and specific data is shown according to there input. 我一直致力于一个应用程序,您可以在其中输入子项名称和年龄,并根据输入显示特定数据。 I store the child info inside a local datastore. 我将子信息存储在本地数据存储区中。

Take a look at this simple class made for simple storage: `public class 看一下这个简单存储的简单类:`public class

 ChildInfo { public String name; public String gradeLevel; public int gradeLevelInt; public ChildInfo() { } public ChildInfo(String _name, String _gradeLevel, int _gradeLevelInt) { gradeLevel = _gradeLevel; name = _name; } } 

Inside my main fragment I have a list of ChildInfo Classes named children . 在我的主要片段中,我有一个名为children的ChildInfo类列表。

I call them like so: 我称之为:

 ParseQuery query = new ParseQuery("Child"); query.fromLocalDatastore(); query.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> objects, ParseException e) { if (e == null) { children.clear(); //Clear it out before we regen every person! for (int i = 0; i < objects.size(); i++) { ChildInfo child = new ChildInfo(); child.name = objects.get(i).getString("Name"); child.gradeLevel = objects.get(i).getString("GradeLevel"); child.gradeLevelInt = objects.get(i).getInt("GradeLevelInt"); children.add(child); } for (int i = pagerAdapter.getCount() - 1; i > 0; i--){ pagerAdapter.removeView(pager, i); } //Removes all pages. } else { Log.e("HomeScreen", e.toString()); } } }); 

The Question 问题

This code WORKS, half the time . 这个代码工作, 一半的时间 When I execute the query in the onViewCreated method it will return nothing. 当我在onViewCreated方法中执行查询时,它将不返回任何内容。 object.size is zero, but when called in the onResume method after I enter another page and exit back to this fragment it WILL work. object.size为零,但是当我进入另一个页面并退回到这个片段之后在onResume方法中调用它时它将起作用。 Any ideas? 有任何想法吗? I think this might be a bug with the parse API. 我认为这可能是解析API的一个错误。

Alright. 好的。 I found the fix! 我找到了修复! For some reason the children list lost everything added to it once it left the done method. 出于某种原因,子列表在离开完成方法后丢失了添加到其中的所有内容。 I re arranged the code and this is the new Parse.com Query 我重新安排了代码,这是新的Parse.com查询

 ParseQuery query = new ParseQuery("Child"); query.fromLocalDatastore(); query.findInBackground(new FindCallback<ParseObject>() { @Override public void done(List<ParseObject> objects, ParseException e) { if (e == null) { removeAllViews(pager); children.clear(); //Clear it out before we regen every person! for (int i = 0; i < objects.size(); i++) { ChildInfo child = new ChildInfo(); child.name = objects.get(i).getString("Name"); child.gradeLevel = objects.get(i).getString("GradeLevel"); child.gradeLevelInt = objects.get(i).getInt("GradeLevelInt"); children.add(child); } Log.i("HomePagerAdapter", "Children Size: " + children.size() + "\\n Obj Size: " + objects.size()); for (int i = 0; i < children.size(); i++) { addChild(pager, children.get(i).name, children.get(i).gradeLevelInt); } } else { Log.e("HomePagerAdapter", e.toString()); } } }); 

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

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