对导航标题中的TextView所做的更改不会更新UI。所有这些都是在UI线程上执行的。TextView的值是特定于用户的,所以我需要在Activity中设置它们。
我的想法是从我的API中获取信息,然后直接显示出来。API部分工作正常,显示不是很多。
我的活动:
class User(val username: String, val display_name: String )
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var mDrawerLayout: DrawerLayout
private lateinit var btnLogout: TextView
val client: OkHttpClient = OkHttpClient()
fun getMyProfile(){
// code that makes the request to my API is here!(and it's working)
override fun onResponse(call: Call, response: Response) {
val body= response.body()?.string()
println(response.code())
println(body)
if(response.code()==200){
val gson = GsonBuilder().create()
val my_user = gson.fromJson(body, User::class.java)
val view = LayoutInflater.from(this@MainActivity).inflate(R.layout.nav_header_main, null, false)
val username: TextView = view.findViewById(R.id.username)
val display_name: TextView = view.findViewById(R.id.displayname)
username.text = my_user.username
display_name.text = my_user.display_name
}
if(response.code()==401){
// if the request is not allowed
}
}
})
}
override fun onCreate(savedInstanceState: Bundle?) {
val context:Context = baseContext
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_new_group, R.id.nav_new_channel,
R.id.nav_contacts, R.id.nav_settings, R.id.nav_send
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
getMyProfile()
...
}
...
}nav_header_main.xml
<TextView
android:id="@+id/displayname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />my_user.username和my_user.display_name都有值(我已经特别检查过了)。
发布于 2019-11-23 17:58:23
我想通了。你必须创建一个充气装置和一个ViewGroup。所以它变成了:
val inflater: LayoutInflater = this@MainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val viewGroup : ViewGroup = findViewById (R.id.nav_view)
var view = inflater.inflate(R.layout.nav_header_main, viewGroup)指向TextViews的变量变成:
var display_name: TextView = view.findViewById(R.id.displayname)
var username: TextView = view.findViewById(R.id.username)https://stackoverflow.com/questions/58984547
复制相似问题