我有一个正在使用的Android应用程序,正在通过Volley发出API请求,在进行API调用后,我得到了一堆返回的JSON对象。我现在将值保存为默认值。保存后,我现在尝试将值显示到ui元素中,但是所有内容均为空白,并且不显示任何值。我的代码在下面突出显示
}
我的MainActivity
fun loginUser(context: Context, email: String, password: String, completion: (Boolean) -> Unit): Unit {
val jsonBody = JSONObject()
jsonBody.put("email", email)
jsonBody.put("password", password)
val requestBody = jsonBody.toString()
val loginRequest = object : JsonObjectRequest(Method.POST, URL_LOGIN, null, Response.Listener {response ->
try {
userEmail = response.getString("user")
authToken = response.getString("token")
isLoggedIn = true
completion(true)
} catch (e: JSONException) {
Log.d("JSON", "EXC:" + e.localizedMessage)
completion(false)
}
}, Response.ErrorListener { error ->
Log.d("ERROR", "Could not login user: $error")
completion(false)
}) {
override fun getBodyContentType(): String {
return "application/json; charset=utf-8"
}
override fun getBody(): ByteArray {
return requestBody.toByteArray()
}
}
Volley.newRequestQueue(context).add(loginRequest)
}
fun findUserByEmail(context: Context, completion: (Boolean) -> Unit): Unit {
val finUserByEmailRequest = object : JsonObjectRequest(Method.GET, "$URL_GET_USER$userEmail", null, Response.Listener {response ->
try {
UserDataService.name = response.getString("name")
UserDataService.email = response.getString("email")
UserDataService.avatarName = response.getString("avatarName")
UserDataService.avatarColor = response.getString("avatarColor")
UserDataService.id = response.getString("_id")
val userDataChanaged = Intent(BROADCAST_USER_DATA_CHANGED)
LocalBroadcastManager.getInstance(context).sendBroadcast(userDataChanaged)
completion(true)
} catch (e: JSONException) {
Log.d("JSON", "EXC:" + e.localizedMessage)
completion(false)
}
}, Response.ErrorListener { error ->
Log.d("ERROR", "Could not find user: $error")
completion(false)
}) {
override fun getBodyContentType(): String {
return "application/json; charset=utf-8"
}
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Authorization"] = "Bearer $authToken"
return headers
}
}
Volley.newRequestQueue(context).add(finUserByEmailRequest)
}
在哪里调用loginUser方法
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
LocalBroadcastManager.getInstance(this).registerReceiver(userDataChangeReceiver, IntentFilter(BROADCAST_USER_DATA_CHANGED))
}
private val userDataChangeReceiver = object : BroadcastReceiver() {
override fun onReceive(contect: Context?, intent: Intent?) {
if (AuthService.isLoggedIn) {
userNameNavHeader.text = UserDataService.name
userEmailNavHeader.text = UserDataService.email
val resourceId = resources.getIdentifier(UserDataService.avatarName, "drawable", packageName)
userImageNavHeader.setImageResource(resourceId)
userImageNavHeader.setBackgroundColor(UserDataService.returnAvatarColor(UserDataService.avatarColor))
loginBtnNavHeader.text = "Logout"
}
}
}
答案 0 :(得分:0)
首先您需要首先获取对nav_header_main布局的引用,以便能够更新nav_header_main中的text-view和image-view。而不是直接使用元素的ID,您应该使用nav_header引用,然后使用ID
private val userDataChangeReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if(AuthService.isLoggedIn){
Log.d("qwe", UserDataService.name)
Log.d("qwe", UserDataService.email)
Log.d("qwe", UserDataService.avatarName)
Log.d("qwe", "Made it inside the broadcast bro")
Log.d("qwe", "Wrap")
nav_drawer_header_include.userEmailNavHeader.text = UserDataService.email
nav_drawer_header_include.userNameNavHeader.text = UserDataService.name
userEmailNavHeader.text = UserDataService.email
val resourseid= resources.getIdentifier(UserDataService.avatarName,"drawable",packageName)
nav_drawer_header_include.userImageNavHeader.setImageResource(resourseid)
nav_drawer_header_include.loginBtnNavHeader.text="Logout"
}
}
}
我们应该最常使用Log,它可以帮助我直接解决问题,而不是将时间浪费在完美的工作上。
在进行Android Kotlin课程时,我也偶然发现了这个问题,但是经过搜索,我发现很多人都遇到了这个问题,他们在不同的平台上提出了问题,但没有提供答案。但是在搜索了相同的术语“ UI元素未更新android”之后,在Java How to change text of a TextView in navigation drawer header?
中发现了一些堆栈溢出答案他们都解释说首先要获得参考,然后我们才能更新View。然后我尝试了,幸运的是,它工作得很好。
PS:这是我的第一个回答,对不起,我深表歉意。