我试图将两个字符串通过一个捆绑包传递给另一个片段。问题是,onCreate中的参数仍然为空。这是我的代码:
class VenueDetailFragment : Fragment() {
private lateinit var venueId: String
private lateinit var venueName: String
companion object {
fun newInstance(venue: Venue): VenueDetailFragment {
val frag = VenueDetailFragment()
val bundle = Bundle()
bundle.putString(Extra.VENUE_ID_KEY, venue.id)
bundle.putString(Extra.VENUE_NAME, venue.name)
frag.arguments = bundle
return VenueDetailFragment()
}
}
private lateinit var viewModel: VenueDetailViewModel
override fun onAttach(context: Context) {
super.onAttach(context)
viewModel = ViewModelProviders.of(this).get(VenueDetailViewModel::class.java)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
venueId = it.getString(Extra.VENUE_ID_KEY, "0")
venueName = it.getString(Extra.VENUE_NAME, getString(R.string.venue_detail_title_unknown))
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.venue_detail_fragment, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.getVenueDetails(venueId).observe(this, Observer { result ->
result?.let { safeVenue ->
venueDetailTitle.text =
if (safeVenue.name.isNotEmpty()) safeVenue.name else venueName
venueDetailDescription.text =
if (safeVenue.description.isNotEmpty()) safeVenue.description
else getString(R.string.venue_detail_description_unknown)
venueDetailAddress.text = safeVenue.location.formattedAddress.joinToString(",\n")
venueDetailRatingBar.rating = (safeVenue.rating / 5).toFloat()
setupContactView(safeVenue.contact)
}
})
}
private fun setupContactView(contact: Contact) {
venueDetailTwitterBtn.visibility = getVisibility(contact.twitter)
venueDetailInstagramBtn.visibility = getVisibility(contact.instagram)
venueDetailFacebookBtn.visibility = getVisibility(contact.facebook)
venueDetailCallBtn.visibility = getVisibility(contact.phone)
}
private fun getVisibility(contactType: String?): Int {
return if (contactType.isNullOrEmpty()) View.VISIBLE else View.GONE
}
}
我通过以下方式初始化新片段:
private fun navigateToDetailPage(data: List<Venue>) {
activity?.let { safeActivity ->
venueResultList.adapter = VenueFinderAdapter(
safeActivity, data,
object : VenueFinderAdapter.VenueSelectionListener {
override fun onVenueSelected(venue: Venue) {
safeActivity.supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainer, VenueDetailFragment.newInstance(venue))
addToBackStack(null)
commit()
}
}
})
}
}
如果运行此命令,则我的两个Lateinit变量(字符串)在onViewCreated中仍为null。我看到另一篇文章告诉我,您应该从onCreate的参数中获取值,您可以看到我也尝试过,但是没有成功。
有人可以告诉我我在做什么错吗?
答案 0 :(得分:6)
您要返回VenueDetailFragment()
,应该返回frag
fun newInstance(venue: Venue): VenueDetailFragment {
val frag = VenueDetailFragment()
val bundle = Bundle()
bundle.putString(Extra.VENUE_ID_KEY, venue.id)
bundle.putString(Extra.VENUE_NAME, venue.name)
frag.arguments = bundle
return frag
}
答案 1 :(得分:-1)
在newInstance方法中返回片段而不是VenueDetailFragment()
fun newInstance(venue: Venue): VenueDetailFragment {
val frag = VenueDetailFragment()
val bundle = Bundle()
bundle.putString(Extra.VENUE_ID_KEY, venue.id)
bundle.putString(Extra.VENUE_NAME, venue.name)
frag.arguments = bundle
return frag
}