Android 工作室 java

Android studio with java

我无法从数据库中获取数据,这里没有数据, 数据不会显示在方法之外。 你能帮帮我吗?

List<Person> Refresh() {
    Person p = new Person();
    ParseQuery <ParseObject> query = ParseQuery.getQuery(NAME_DATABASE);
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> scoreList , ParseException e) {
            if (e == null) {
                for (int i = 0; i < scoreList.size(); i++){
                    p.setId(scoreList.get(i).getInt(Key.ID));
                    p.setName(scoreList.get(i).getString(Key.NAME));
                    p.setAge(scoreList.get(i).getString(Key.AGE));                       p.setDate_start(scoreList.get(i).getString(Key.DATE_START));
                    p.setMonth_number(scoreList.get(i).getString(Key.MONTH_NUMBER));
                    p.setPropriety(scoreList.get(i).getString(Key.PROPRIETY));
                    p.setPrice(scoreList.get(i).getString(Key.PRICE));
                    p.setGender(scoreList.get(i).getString(Key.GENDER));                       
                    persons.add(p); //there is find data
                }
            }
            else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });
    return persons; //here there isn't data (size=0)
}

答案真的很简单。您正在返回 findInBackground() 方法之外的人员。

    else{
     Log.d("score", "Error: " + e.getMessage());
    }
     //Return here after the else statement
     return persons;
    }

注意:您可能希望将 persons 设为全局变量,否则 android studio 会告诉您将其声明为最终变量。