[英]Initial user first name is displaying remaining first name are empty in android
嗨,在下面我有兩項活動,一項用於登錄和主要活動。 一旦使用正確的用戶名和密碼登錄成功,它將在從登錄移動到主活動時移動到主活動我正在使用意圖將用戶名傳遞給主活動。
在主要活動中,我調用 APi。從 Api 我收到響應等於然后我取該用戶的第一個名稱並將其設置為 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 ;
}
Model Class:
public class Records {
@SerializedName("user_name")
@Expose
private String user_name;
@SerializedName("title")
@Expose
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Records(String id,String user_name,String first_name,String last_name,String email,String title) {
this.user_name = user_name;
this.title = title;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.id = id;
}
@SerializedName("first_name")
@Expose
private String first_name;
@SerializedName("last_name")
@Expose
private String last_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail1() {
return email;
}
public void setEmail1(String email1) {
this.email = email1;
}
@SerializedName("email1")
@Expose
private String email;
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@SerializedName("id")
@Expose
private String id;
}
您不能從后台進程或任務(如后台網絡調用)為任何TextView
設置值。 由於您使用的是firstname.setText(first_names);
在后台網絡調用中它不起作用。 在您的ViewModel
中使用MutableLiveData
,然后從您的Activity
中觀察它,並在您的觀察者內部更新您的TextView
,如下所示:
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...
}
從您的Activity
中觀察您的firstName
的值,並使用最新的值更新您的TextView
,如下所示:
public class YourActivity extends AppCompatActivity {
private ViewModel model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get an instance of ViewModel.
model = new ViewModelProvider(this).get(ViewModel.class);
// Create the observer which updates the UI.
final Observer<String> firstNameObserver = new Observer<String>() {
@Override
public void onChanged(@Nullable final String newFirstName) {
// Update the TextView here with latest value
firstname.setText(newFirstName);
}
};
// Observe the MutableLiveData, passing this activity as the LifecycleOwner to the observer.
model.getCurrentName().observe(this, nameObserver);
}
}
最后,當您從服務器獲取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);
}
}
}
然后,您的TextView
將使用從您的 API 調用中獲得的最新值正確更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.