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 “初始用户名”显示的是android中剩余的“用户名”为空_Java_Android - Fatal编程技术网

Java “初始用户名”显示的是android中剩余的“用户名”为空

Java “初始用户名”显示的是android中剩余的“用户名”为空,java,android,Java,Android,您好,在下面我有两个活动,一个是登录和主。一旦使用正确的用户名和密码登录成功,当从登录移动到主活动时,它将移动到mainactivity。我正在使用intent将用户名传递到mainactivity 在主活动中,我调用APi。在响应成功后,我将从APi获得响应,我可以获得不同的字符串。从中我将获得名字,用户名。现在从loginActivity用户名和mainactivity用户名。如果两个名称相等,则我将取该用户的第一个名称,并将其设置为textview 谁能帮我一下我哪里出了错 对于初始用户,

您好,在下面我有两个活动,一个是登录和主。一旦使用正确的用户名和密码登录成功,当从登录移动到主活动时,它将移动到mainactivity。我正在使用intent将用户名传递到mainactivity

在主活动中,我调用APi。在响应成功后,我将从APi获得响应,我可以获得不同的字符串。从中我将获得名字,用户名。现在从loginActivity用户名和mainactivity用户名。如果两个名称相等,则我将取该用户的第一个名称,并将其设置为textview

谁能帮我一下我哪里出了错

对于初始用户,我可以看到名字,然后如果我用另一个用户名登录,那么这个名字是空的

MainActivity.java:

username = getIntent().getStringExtra("username");
private void fetchUserJSON(){

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

             sessionId = getIntent().getStringExtra("sessionId");
            //username = getIntent().getStringExtra("username");
            String operation = "query";
            String query = "select  *  from Users";
            final GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);
            /** Call the method with parameter in the interface to get the notice data*/
            Call<UserModule> call = service.UserRecordDetails(operation, sessionId, query);
            /**Log the URL called*/
            Log.i("URL Called", call.request().url() + "");
            call.enqueue(new Callback<UserModule>() {
                @Override
                public void onResponse(Call<UserModule> call, Response<UserModule> response) {
                    Log.e("response", new Gson().toJson(response.body()));
                    if (response.isSuccessful()) {
                        Log.e("response", new Gson().toJson(response.body()));
                        UserModule userModule = response.body();
                        String success = userModule.getSuccess();
                        if (success.equals("true")) {
                            Results_Users results = userModule.getResult();
                            records = results.getRecords();
                            for (Records records1 : records) {
                                String user_name = records1.getUser_name();
                                String id = records1.getId();
                                Log.d("id", id);
                                String first_name = records1.getFirst_name();
                                Log.d("first_name", first_name);

                                String last_name = records1.getLast_name();
                                String email1 = records1.getEmail1();
                                String title = records1.getTitle();
                                Records records2 = new Records(user_name, title, first_name, last_name, email1, id);
                                recordsList.add(records2);
                                ArrayList<String> records_lis=new ArrayList<>();
                                records_lis.add(recordsList.toString());
                                Log.d("records_lis", String.valueOf(records_lis.size()));
                                Log.d("size", String.valueOf(recordsList.size()));
                                for (int i = 0; i < recordsList.size(); i++)
                                    if (username.equalsIgnoreCase(user_name)) {
                                        String first_names = recordsList.get(0).getFirst_name();
                                        firstname.setText(first_names);
                                    }
                            }
                        }
                    }
                }
                @Override
                public void onFailure(Call<UserModule> call, Throwable t) {
                }
                //     progressDialog.dismiss();
            });
        }
    }, 0);
    return ;
}

不能从后台进程或任务(如后台网络调用)将值设置为任何
TextView
。因为您使用的是
firstname.setText(firstname)在后台网络调用中,它将不起作用。在您的
视图模型中使用
MutableLiveData
,然后从您的
活动中观察它,并在您的观察者内部更新您的
文本视图
,如:

firstname.setText(first_names);
当您从API中获取一个值,然后将其设置为
MutableLiveData
,它将自动更新您的“TextView”

ViewModel
中创建一个
MutableLiveData
,如下所示:

public class MyViewModel extends ViewModel {
    // Create a LiveData with a String value
    private MutableLiveData<String> firstName;

    public MutableLiveData<String> getFirstName() {
        if (firstName == null) {
            firstName = new MutableLiveData<String>();
        }
        return firstName;
    }

    // Rest of the ViewModel below...
}
最后,当您从服务器获得
firstName
时,将其发送到名为
firstName
MutableLiveData
以更新您的
TextView
,如下所示:

if (response.isSuccessful()) {
    Log.e("response", new Gson().toJson(response.body()));
    UserModule userModule = response.body();
    String success = userModule.getSuccess();
    if (success.equals("true")) {
    Results_Users results = userModule.getResult();
    records = results.getRecords();
    for (Records records1 : records) {
        String user_name = records1.getUser_name();
        String id = records1.getId();
        Log.d("id", id);
        String first_name = records1.getFirst_name();
        Log.d("first_name", first_name);

        String last_name = records1.getLast_name();
        String email1 = records1.getEmail1();
        String title = records1.getTitle();
        Records records2 = new Records(user_name, title, first_name, last_name, email1, id);
        recordsList.add(records2);
        ArrayList<String> records_lis=new ArrayList<>();
        records_lis.add(recordsList.toString());
        Log.d("records_lis", String.valueOf(records_lis.size()));
        Log.d("size", String.valueOf(recordsList.size()));
        for (int i = 0; i < recordsList.size(); i++)
            if (username.equalsIgnoreCase(user_name)) {
                String first_names = recordsList.get(0).getFirst_name();
                model.getCurrentName().postValue(first_names);
            }
    }
}
if(response.issusccessful()){
Log.e(“response”,new Gson().toJson(response.body());
UserModule UserModule=response.body();
字符串success=userModule.getSuccess();
if(success.equals(“true”)){
Results_Users Results=userModule.getResult();
记录=结果。getRecords();
用于(记录1:记录){
字符串user_name=records1.getUser_name();
String id=records1.getId();
Log.d(“id”,id);
String first_name=records1.getFirst_name();
Log.d(“名字”,名字);
字符串last_name=records1.getLast_name();
字符串email1=records1.getEmail1();
字符串title=records1.getTitle();
Records records2=新记录(用户名、标题、名、姓、email1、id);
recordsList.add(记录2);
ArrayList记录=新的ArrayList();
records_lis.add(recordsList.toString());
Log.d(“records_lis”,String.valueOf(records_lis.size());
Log.d(“size”,String.valueOf(recordsList.size());
对于(int i=0;i

然后,您的
TextView
将使用从API调用中获取的最新值正确更新。

您不能像后台网络调用一样从后台进程或任务将值设置为任何
TextView
。因为您使用的是
firstname.setText(first\u names)
在后台网络调用中,它将不起作用。在
视图模型中使用
可变LiveData
,然后从
活动中观察它,并在观察者内部更新
文本视图
,如:

firstname.setText(first_names);
当您从API中获取一个值,然后将其设置为
MutableLiveData
,它将自动更新您的“TextView”

ViewModel
中创建一个
MutableLiveData
,如下所示:

public class MyViewModel extends ViewModel {
    // Create a LiveData with a String value
    private MutableLiveData<String> firstName;

    public MutableLiveData<String> getFirstName() {
        if (firstName == null) {
            firstName = new MutableLiveData<String>();
        }
        return firstName;
    }

    // Rest of the ViewModel below...
}
最后,当您从服务器获得
firstName
时,将其发送到名为
firstName
MutableLiveData
以更新您的
TextView
,如下所示:

if (response.isSuccessful()) {
    Log.e("response", new Gson().toJson(response.body()));
    UserModule userModule = response.body();
    String success = userModule.getSuccess();
    if (success.equals("true")) {
    Results_Users results = userModule.getResult();
    records = results.getRecords();
    for (Records records1 : records) {
        String user_name = records1.getUser_name();
        String id = records1.getId();
        Log.d("id", id);
        String first_name = records1.getFirst_name();
        Log.d("first_name", first_name);

        String last_name = records1.getLast_name();
        String email1 = records1.getEmail1();
        String title = records1.getTitle();
        Records records2 = new Records(user_name, title, first_name, last_name, email1, id);
        recordsList.add(records2);
        ArrayList<String> records_lis=new ArrayList<>();
        records_lis.add(recordsList.toString());
        Log.d("records_lis", String.valueOf(records_lis.size()));
        Log.d("size", String.valueOf(recordsList.size()));
        for (int i = 0; i < recordsList.size(); i++)
            if (username.equalsIgnoreCase(user_name)) {
                String first_names = recordsList.get(0).getFirst_name();
                model.getCurrentName().postValue(first_names);
            }
    }
}
if(response.issusccessful()){
Log.e(“response”,new Gson().toJson(response.body());
UserModule UserModule=response.body();
字符串success=userModule.getSuccess();
if(success.equals(“true”)){
Results_Users Results=userModule.getResult();
记录=结果。getRecords();
用于(记录1:记录){
字符串user_name=records1.getUser_name();
String id=records1.getId();
Log.d(“id”,id);
String first_name=records1.getFirst_name();
Log.d(“名字”,名字);
字符串last_name=records1.getLast_name();
字符串email1=records1.getEmail1();
字符串title=records1.getTitle();
Records records2=新记录(用户名、标题、名、姓、email1、id);
recordsList.add(记录2);
ArrayList记录=新的ArrayList();
records_lis.add(recordsList.toString());
Log.d(“records_lis”,String.valueOf(records_lis.size());
Log.d(“size”,String.valueOf(recordsList.size());
对于(int i=0;i

然后,您的
TextView
将使用从API调用获取的最新值正确更新。

您得到了什么错误?当您与用户名进行比较时,您正在执行
recordsList.get(0)
以及当您从
recordsList.get(2)获取用户名时
,一个来自第0个索引,一个来自第2个索引。将您的错误发布为well@RajenRaiyarela我没有收到任何错误。例如,如果我以admin的用户名登录,那么我想显示admin用户的名字,但它是prinitingempty@Dharmaraj我没有收到任何错误First_name只显示First name您收到了什么错误?当您与用户名进行比较时,您正在进行
记录