Return语句未返回正确的值

时间:2018-07-24 03:13:05

标签: java android

public static String getFirstLangScore(CRSDatabase db) {
         String firstLanguageScore = "";
        if (mGlobals.getSelectLanguage().equals("ENGLISH")) {
            String[] lowestEnglishReading = mGlobals.getEnglishReadingScore().split("CLB ");            
            firstLanguageScore = //did some calculation and updated the value

        }
        else if(mGlobals.getSelectLanguage().equals("FRENCH")){
            String[] lowestFrenchReading = mGlobals.getFrenchReadingScore().split("CLB ");

            firstLanguageScore = // did some calculation and updated the value

        }
        return firstLanguageScore;  // here it is returning the initial initialized value.

    }

我正在从asynctask调用此方法,但是它没有返回更新后的值,而是返回了我的“”,这是方法开始时的初始化值。

1 个答案:

答案 0 :(得分:1)

是因为您使用异步线程来处理数据,但您确实要在主线程中获取数据,但是您在主线程中打开了此任务,所以异步线程尚未获得相应的值,主线程得到了这个变量

请尝试

public void getFirstLangScore(CRSDatabase db,DataCallback dataBack) { 

String firstLanguageScore = "";

if (mGlobals.getSelectLanguage().equals("ENGLISH")) {
    String[] lowestEnglishReading = mGlobals.getEnglishReadingScore().split("CLB ");


    firstLanguageScore = //did some calculation and updated the value

}
else if(mGlobals.getSelectLanguage().equals("FRENCH")){
    String[] lowestFrenchReading = mGlobals.getFrenchReadingScore().split("CLB ");

    firstLanguageScore = // did some calculation and updated the value

}
 databack.returnLanguageScore(firstLanguageScore );
}

/**
*DataCallBack
*/
public interface DataCallback {
   void returnLanguageScore(String data);
}

在主线程中调用异步方法,然后传入DataCallBack。实现其接口,等待异步回调, 或者您可以使用Handler处理