Kotlin片段中的API数据 [英] API data in kotlin fragment
问题描述
我正在尝试使用Drawer activity
,似乎它有3个文件,只是将简单文本显示为This is home Fragment
:|
I am trying to use Drawer activity
, seems that it has 3 files to just show simple text as This is home Fragment
:|
无论如何,我已经追溯了所有这些文件并找到了fragment_home.xml
,HomeFragment.kt
和HomeViewModel.kt
Anyway, I have tracked back all those files and found fragment_home.xml
, HomeFragment.kt
and HomeViewModel.kt
问题
我应该如何调用API数据槽片段?
代码
my code
基于 android studio文档,这是我应该按顺序使用的代码获取我的api数据.
Based on android studio documentation this is the code that I should use in order to get my api data.
val textView = findViewById<TextView>(R.id.TextView)
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "https://example.com/api/listings"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
textView.text = "Response is: ${response.substring(0, 500)}"
},
Response.ErrorListener { textView.text = "That didn't work!" })
// Add the request to the RequestQueue.
queue.add(stringRequest)
PS:我在上面的空活动中尝试了此代码,
PS: I have tried this code above in empty activity it's working
HomeViewModel.kt
class HomeViewModel : ViewModel() {
private val _text = MutableLiveData<String>().apply {
value = "This is home Fragment"
}
val text: LiveData<String> = _text
}
HomeFragment.kt
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(this, Observer {
textView.text = it
})
return root
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
请注意,在尝试拒绝投票之前:我知道这个问题可能是非常基本的问题,但是请注意,这是我的第一个问题 曾经使用片段(基本上是android studio中的新手),所以是的 我的问题有点基本:)
Please note, before you try to give down-vote: I am aware this question probably is very basic question, but note that it's my first time ever using fragments (basically newbie in android studio), so yes my questions are kind of basic :)
推荐答案
要从片段中调用API,您可以
for calling the API from fragment, you can do
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
//calling the API
callAPIDemo(textView)
// homeViewModel.text.observe(this, Observer {
// textView.text = it
// })
return root
}
fun callAPIDemo(textView: TextView) {
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(activity)
val url = "https://example.com/api/listings"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
textView.text = "Response is: ${response.substring(0, 500)}"
},
Response.ErrorListener { textView.text = "That didn't work!" })
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
}
这篇关于Kotlin片段中的API数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!