简体   繁体   English

从 Listview (Fragment) 的 onClick 方法启动 Activity

[英]Launch Activity from onClick method of Listview (Fragment)

I recently started learning Android development in Kotlin.我最近开始在 Kotlin 中学习 Android 开发。 I did follow this guide and everything went good.我确实遵循了指南,一切都很顺利。

Now I'm trying to merge the content of those two guides:现在我正在尝试合并这两个指南的内容:
https://developer.android.com/training/contacts-provider/retrieve-details.html#kotlin https://developer.android.com/training/contacts-provider/retrieve-details.html#kotlin
and
https://developer.android.com/guide/components/fragments#Example https://developer.android.com/guide/components/fragments#Example

in order to display the details of a Contact using Fragments.为了使用片段显示联系人的详细信息。 I'm having trouble to launch an activity in the onItemClick method (the guide uses a ListView):我无法在onItemClick方法中启动活动(指南使用 ListView):

 override fun onItemClick(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
    // Get the Cursor
    val cursor: Cursor? = (parent.adapter as? CursorAdapter)?.cursor?.apply {
        // Move to the selected contact
        moveToPosition(position)
        // Get the _ID value
        contactId = getLong(Companion.CONTACT_ID_INDEX)
        // Get the selected LOOKUP KEY
        //contactKey = getString(CONTACT_KEY_INDEX)
        mContactKey = getString(Companion.CONTACT_KEY_INDEX)
        // Create the contact's content Uri
        contactUri = ContactsContract.Contacts.getLookupUri(contactId, mContactKey)
        /*
         * You can use contactUri as the content URI for retrieving
         * the details for a contact.
         */
    }
    val intent = Intent().apply{
        setClass(activity,DetailsActivity::class.java)
        putExtra("contactID",contactId)
        putExtra("mContackKey",mContactKey)
        putExtra("contactUri",contactUri)
    }
    startActivity(intent)
}

If I create the Intent to start the activity as displayed in the guide, I get the compiler error " Inferred type is FragmentActivity?, but context was expected ".如果我创建 Intent 以启动指南中显示的活动,我会收到编译器错误“推断类型是 FragmentActivity?,但需要上下文”。

I changed then the Intent to either one of the following:然后我将 Intent 更改为以下任一项:

val intent = Intent().apply{
        setClass(requireContext(),DetailsActivity::class.java)
        putExtra("contactID",contactId)
        putExtra("mContackKey",mContactKey)
        putExtra("contactUri",contactUri)
    }
    startActivity(intent)

or或者

 val intent = Intent().apply{
        context?.let { setClass(it,DetailsActivity::class.java) }
        putExtra("contactID",contactId)
        putExtra("mContackKey",mContactKey)
        putExtra("contactUri",contactUri)
    }
    startActivity(intent)

whit this, I do not get the compiler error, but in the Logcat I see the notice " W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@9dc9013 "对此,我没有收到编译器错误,但在 Logcat 中我看到了通知“ W/ActivityThread:handleWindowVisibility:token android.os.BinderProxy@9dc9013 没有活动

Can you please point me to the correct way to instantiate an activity from the onClick method of a ListView inside a Fragment ?您能否指出从 Fragment 内 ListView 的 onClick 方法实例化活动的正确方法? Thank you!谢谢!

On a related note: do you recommend those guides or is their content obsolete ?相关说明:您是推荐这些指南还是它们的内容已过时?

Edit: the full fragment class is here编辑:完整的片段类在这里

Try below code for opening new activity from fragment :尝试以下代码以从片段打开新活动:

activity?.let{
    val intent = Intent (it, DetailsActivity::class.java)
    intent.putExtra("contactID",contactId);
    intent.putExtra("mContackKey",mContactKey);
    intent.putExtra("contactUri",contactUri);
    it.startActivity(intent)
}

startActivity() is a function in the Context class (of which Activity is a subclass), not of the Fragment class. startActivity()是 Context 类(其中 Activity 是子类)中的函数,而不是 Fragment 类中的函数。

So you can make a direct bare call to startActivity() from within the code of a subclass of Context (as any Activity implementation is), but when you are calling it from a Fragment, you have to call it on a context: context.startActivity() .因此,您可以从 Context 的子类的代码中直接对startActivity()进行裸调用(就像任何 Activity 实现一样),但是当您从 Fragment 调用它时,您必须在上下文中调用它: context.startActivity()

The Fragment.context property is nullable, so you can use requireContext().startActivity() . Fragment.context属性可为空,因此您可以使用requireContext().startActivity() It won't be null when responding to clicks, so this is safe.响应点击时它不会为空,所以这是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM