如何将 API 中的项目加载到 Activity 中的片段?
How do I load an item from API to a fragment inside an Activity?
我需要将 API 中的元素加载到 activity 中的片段,但是一旦我初始化 Activity Class 中的元素像 TextView 这样的片段应用程序崩溃了。
This is what the property Profile Activity looks like
这是 PropertyProfile Kotlin 代码
val item = intent.getSerializableExtra("item") as DemoDataBaseItem
val url = intent.getStringExtra("url")
aboutp = findViewById(R.id.frag_details_text)
locationp = findViewById(R.id.profile_location)
profilenametext = findViewById(R.id.profile_property_name)
profileratingbar = findViewById(R.id.profile_rating_bar)
profileimageview = findViewById(R.id.imv_property_profile)
bnow_button = findViewById(R.id.booknow_btn)
call = findViewById(R.id.call_btn)
aboutp.text = item.about_property
locationp.text = item.location_country
profilenametext.text = item.property_name
profileratingbar.rating = item.rating_bar.toFloat()
Glide.with(this).load(item.imageurl).centerCrop()
.into(profileimageview)
call.setOnClickListener {
//open the call intent
val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", item.property_phone, null))
startActivity(intent)
}
bnow_button.setOnClickListener {
val intent = Intent(this, Webview::class.java)
val finalURL = "${item.beurl}#checkin=${checkinText.text}&checkout=${checkoutText.text}"
intent.putExtra("item", item)
intent.putExtra("url", finalURL)
startActivity(intent)
}
这是 Main 中的代码 Activity
fun getAllData(){
Api.retrofitService.getAllData().enqueue(object: Callback<List<DemoDataBaseItem>>{
override fun onResponse(
call: Call<List<DemoDataBaseItem>>,
response: Response<List<DemoDataBaseItem>>
) {
if(response.isSuccessful){
datalist = response.body()!!.toMutableList()
mAdapter = MyAdapter(response.body()!!, this@MainActivity)
recyclerView.adapter = mAdapter
}
}
//required DO NOT REMOVE, this is for error tracking
override fun onFailure(call: Call<List<DemoDataBaseItem>>, t: Throwable) {
}
})
}
//additional url adding
override fun onClick(item: DemoDataBaseItem) {
val intent = Intent(this, PropertyProfile::class.java)
intent.putExtra("item", item)
startActivity(intent)
}
}
interface OnClickListener {
fun onClick(item: DemoDataBaseItem)
}
片段代码
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cloudbeds.dummyapitest1.R
class Details : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_details, container, false)
}
}
我需要将 API 中的“about_property”项加载到片段中的文本视图
因为我没有异常堆栈跟踪,所以我建议您将它传递到 Bundle 中,因为我会说发生此转换时会发生异常:
val item = intent.getSerializableExtra("item") as DemoDataBaseItem
尝试按如下方式使用捆绑包发送数据:
val intent = Intent(this, FontAwesomeChip::class.java).apply {
putExtras(
bundleOf(
"item" to $YOUR_ITEM,
"url" to $YOUR_URL
)
)
}
startActivity(intent)
要获取你想要的 Activity 中的数据,使用 onNewIntent Activity 方法如下:
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent?.hasExtra("item") == true && intent.hasExtra("url")) {
val item = intent.getSerializableExtra("item") as $YOUR_CLASS_TYPE
val url = intent.getStringExtra("url")
//textView.text = item.property for example
}
}
我想我没有忘记任何事情,应该工作:D
我需要将 API 中的元素加载到 activity 中的片段,但是一旦我初始化 Activity Class 中的元素像 TextView 这样的片段应用程序崩溃了。
This is what the property Profile Activity looks like
这是 PropertyProfile Kotlin 代码
val item = intent.getSerializableExtra("item") as DemoDataBaseItem
val url = intent.getStringExtra("url")
aboutp = findViewById(R.id.frag_details_text)
locationp = findViewById(R.id.profile_location)
profilenametext = findViewById(R.id.profile_property_name)
profileratingbar = findViewById(R.id.profile_rating_bar)
profileimageview = findViewById(R.id.imv_property_profile)
bnow_button = findViewById(R.id.booknow_btn)
call = findViewById(R.id.call_btn)
aboutp.text = item.about_property
locationp.text = item.location_country
profilenametext.text = item.property_name
profileratingbar.rating = item.rating_bar.toFloat()
Glide.with(this).load(item.imageurl).centerCrop()
.into(profileimageview)
call.setOnClickListener {
//open the call intent
val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", item.property_phone, null))
startActivity(intent)
}
bnow_button.setOnClickListener {
val intent = Intent(this, Webview::class.java)
val finalURL = "${item.beurl}#checkin=${checkinText.text}&checkout=${checkoutText.text}"
intent.putExtra("item", item)
intent.putExtra("url", finalURL)
startActivity(intent)
}
这是 Main 中的代码 Activity
fun getAllData(){
Api.retrofitService.getAllData().enqueue(object: Callback<List<DemoDataBaseItem>>{
override fun onResponse(
call: Call<List<DemoDataBaseItem>>,
response: Response<List<DemoDataBaseItem>>
) {
if(response.isSuccessful){
datalist = response.body()!!.toMutableList()
mAdapter = MyAdapter(response.body()!!, this@MainActivity)
recyclerView.adapter = mAdapter
}
}
//required DO NOT REMOVE, this is for error tracking
override fun onFailure(call: Call<List<DemoDataBaseItem>>, t: Throwable) {
}
})
}
//additional url adding
override fun onClick(item: DemoDataBaseItem) {
val intent = Intent(this, PropertyProfile::class.java)
intent.putExtra("item", item)
startActivity(intent)
}
}
interface OnClickListener {
fun onClick(item: DemoDataBaseItem)
}
片段代码
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cloudbeds.dummyapitest1.R
class Details : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_details, container, false)
}
}
我需要将 API 中的“about_property”项加载到片段中的文本视图
因为我没有异常堆栈跟踪,所以我建议您将它传递到 Bundle 中,因为我会说发生此转换时会发生异常:
val item = intent.getSerializableExtra("item") as DemoDataBaseItem
尝试按如下方式使用捆绑包发送数据:
val intent = Intent(this, FontAwesomeChip::class.java).apply {
putExtras(
bundleOf(
"item" to $YOUR_ITEM,
"url" to $YOUR_URL
)
)
}
startActivity(intent)
要获取你想要的 Activity 中的数据,使用 onNewIntent Activity 方法如下:
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent?.hasExtra("item") == true && intent.hasExtra("url")) {
val item = intent.getSerializableExtra("item") as $YOUR_CLASS_TYPE
val url = intent.getStringExtra("url")
//textView.text = item.property for example
}
}
我想我没有忘记任何事情,应该工作:D