Android 如何从“活动”更新导航标题中的文本视图?
对导航标题中文本视图的更改不会更新UI。所有这些都在UI线程上执行。TextView的值是特定于用户的,因此我需要从活动中设置它们 想法是从我的API中获取信息,然后显示它。API部分正在工作,显示的内容不多 我的活动:Android 如何从“活动”更新导航标题中的文本视图?,android,kotlin,textview,navigation-drawer,Android,Kotlin,Textview,Navigation Drawer,对导航标题中文本视图的更改不会更新UI。所有这些都在UI线程上执行。TextView的值是特定于用户的,因此我需要从活动中设置它们 想法是从我的API中获取信息,然后显示它。API部分正在工作,显示的内容不多 我的活动: class User(val username: String, val display_name: String ) class MainActivity : AppCompatActivity() { private lateinit var appBarC
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都有值(我已经专门检查过了)。我猜出来了。
您必须创建充气器和视图组。
因此,它变成:
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)
指向文本视图的变量变为:
var display_name: TextView = view.findViewById(R.id.displayname)
var username: TextView = view.findViewById(R.id.username)
除非您向某个父级添加
视图
,否则您的代码将不会产生任何效果;您只需膨胀视图,更新它,然后将其抛出即可。较大的代码是什么样子的?发布整个活动代码。