Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中等待函数完成?_Java_Android_Android Studio - Fatal编程技术网

如何在Java中等待函数完成?

如何在Java中等待函数完成?,java,android,android-studio,Java,Android,Android Studio,我正在用Java开发一个Android应用程序,需要从远程数据库加载一些数据。为此,我有一个名为getCards()的函数,它读取数据库并将一些信息加载到SharedReferences中。在我的主要功能中,我想阅读SharedReferences中写的信息,并将它们写在TextView上。问题是,当我试图从SharedReferences获取这些信息时,它们似乎是空的,因为线程没有等待“getCards()”完成。如果我进入另一个活动,然后返回主活动,数据将被放入,但当我第一次启动活动时,从S

我正在用Java开发一个Android应用程序,需要从远程数据库加载一些数据。为此,我有一个名为
getCards()
的函数,它读取数据库并将一些信息加载到
SharedReferences
中。在我的主要功能中,我想阅读
SharedReferences
中写的信息,并将它们写在
TextView
上。问题是,当我试图从
SharedReferences
获取这些信息时,它们似乎是空的,因为线程没有等待“getCards()”完成。如果我进入另一个活动,然后返回主活动,数据将被放入,但当我第一次启动活动时,从
SharedReferences
读取的速度太快。有什么建议吗?我试过用try/catch包围wait(100),但是wapp会崩溃

这是主活动中的代码

        getCards();
       
        ccnr = pref.getString("ccnumber",null);
        ccfn = pref.getString("fullname",null);
        if(ccfn == null || ccnr ==null){
            tvccnumber.setText("No card was added!");
            tvccfullname.setText("No card was added!");

        }else{
            tvccnumber.setText(ccnr);
            tvccfullname.setText(ccfn);
        }
这是
getCards()
的一部分,用于加载
SharedReferences
中的信息

public void getCards(){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GETC_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if(!response.contains("Error!")){
                            JSONArray jsonArray;
                            JSONObject jsonObject;
                            String ccnumber;
                            String fullname;
                            String ccsignature;
                            try {
                                jsonArray = new JSONArray(response);
                                jsonObject = jsonArray.getJSONObject(0);
                                ccnumber = jsonObject.getString("ccNumber");
                                ccsignature = jsonObject.getString("ccSign");
                                fullname = jsonObject.getString("fullName");
                                editor.putString("ccnumber",ccnumber);
                                editor.putString("ccsignature",ccsignature);
                                editor.putString("fullname",fullname);
                                editor.commit();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }else{
                            Toast.makeText(FinalUserSpace.this, response, Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
            
public void getCards(){
StringRequest StringRequest=新的StringRequest(Request.Method.POST,Constants.GETC_URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
如果(!response.contains(“Error!”){
JSONArray JSONArray;
JSONObject JSONObject;
字符串编号;
字符串全名;
字符串签名;
试一试{
jsonArray=新jsonArray(响应);
jsonObject=jsonArray.getJSONObject(0);
ccnumber=jsonObject.getString(“ccnumber”);
ccsignature=jsonObject.getString(“ccSign”);
fullname=jsonObject.getString(“fullname”);
编辑器.putString(“ccnumber”,ccnumber);
编辑器.putString(“ccsignature”,ccsignature);
编辑器.putString(“全名”,全名);
commit();
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Toast.makeText(FinalUserSpace.this,response,Toast.LENGTH_SHORT.show();
}
}
},new Response.ErrorListener(){

您可以使用java对象锁定,直到函数返回

private Object lock = new Object();

您可以使用java对象锁定,直到函数返回

private Object lock = new Object();

使用if-else语句,如say(这是伪代码)if(sharedpreferences=null){dosomething}else{getCards}if语句应使其仅在共享优先级加载数据后才加载数据,然后将数据发送到共享优先级,然后加载到textView如何知道数据已加载?我已尝试使用函数getCards()boolean并在完成时返回true,然后在主函数中检查返回,但在写入SharedReferences之前仍返回。将完成代码放入完成回调中?使用if-else语句,如say(这是伪代码)if(SharedReferences=null){do something}else{getCards}if语句应使其仅在共享优先级加载数据后才加载数据,然后将数据发送到共享优先级,然后加载到textView如何知道数据已加载?我已尝试使用函数getCards()boolean并在完成时返回true,然后在主函数中检查返回,但在写入SharedReferences之前仍返回。是否将完成代码放入完成回调中?
getcards();
       
lock.wait();

ccnr = pref.getString("ccnumber",null);
ccfn = pref.getString("fullname",null);
if(ccfn == null || ccnr ==null){
    tvccnumber.setText("No card was added!");
    tvccfullname.setText("No card was added!");
}else{
    tvccnumber.setText(ccnr);
    tvccfullname.setText(ccfn);
}