mk4
mk4

Reputation: 93

How get a new activity from recycler view in kotlin using androidx.recyclerview.widget.RecyclerView?

I'm trying to start a new Activity from a RecyclerView, doing some computations on it, returning back to the Activity that host that RecyclerView.

How can I do it?

For now in onClick function I create a AlertDialog to "show" the content as if I'm showing an Activity, but this is not correct, I would move as If pressing a button I create an Intent that let me start a new activity giving me the possibility to save then the Movie data in the RecyclerView itself

override fun onClick(it: View) {
        val builder = AlertDialog.Builder(c!!)
        val dialogView = LayoutInflater.from(c!!).inflate(R.layout.movie_createupdate,null)
        if (it.id==R.id.fab) {
            builder.setTitle("Create a new Movie ")
            builder.setView(dialogView)
            dialogView.edit_title.hint="Movie title"
            dialogView.edit_year.hint="year"
            dialogView.edit_description.hint="description"
            builder.setPositiveButton("Create", DialogInterface.OnClickListener { dialog, which ->
                repository.Create(
                    title=dialogView.edit_title.text.toString(),
                    rate=5,
                    year=dialogView.edit_year.text.toString(),
                    description=dialogView.edit_description.text.toString())
            }
            )
        }
        else {
            builder.setTitle("Update " + it.title.text)
            builder.setView(dialogView)
            dialogView.edit_title.text.insert(0, it.title.text)
            dialogView.edit_year.text.insert(0, it.year.text)
            dialogView.edit_description.text.insert(0, it.description.text)
            val pos = it.position.text.toString().toInt()
            builder.setPositiveButton(
                "Update",
                DialogInterface.OnClickListener { dialog, which ->
                    repository.Update(
                        position = pos,
                        title = dialogView.edit_title.text.toString(),
                        rate = 5,
                        year = dialogView.edit_year.text.toString(),
                        description = dialogView.edit_description.text.toString()
                    )

                }
            )
        }
        builder.setNegativeButton("Cancel", null);
        builder.create().show()

    }

And this is the code that contains the recycler view:

<?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"
                                                   xmlns:tools="http://schemas.android.com/tools"
                                                   android:layout_width="match_parent"
                                                   android:layout_height="match_parent"
                                                   tools:context=".activities.LoggedUserActivity">
    <LinearLayout
            android:id="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="#444444"
            android:padding="@dimen/default_padding"
            android:orientation="vertical">

        <LinearLayout
                android:layout_width="354dp"
                android:layout_height="642dp"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="@dimen/default_padding">

            <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/list"
                    android:layout_width="327dp"
                    android:layout_height="475dp" />

            <!-- android:onClick="launchWorkOutActivity"-->
            <Button
                    android:id="@+id/add_workout"
                    android:layout_width="180dp"
                    android:layout_height="wrap_content"
                    android:text = "add film"
                    android:textAllCaps="false"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toStartOf="parent" />
        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 81

Answers (2)

Guru Prasad mohapatra
Guru Prasad mohapatra

Reputation: 1969

you can just use startActivityForResult() .Then create a onActivityResult(int requestCode, int resultCode, Intent data) in your adapter class to handle the response.Then you can call it from the onActivityResult of the current activity.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        adapter.onActivityResult(requestCode, resultCode, data);
        //adapter is the object of the Adapter class for your recyclerview
    }

Upvotes: 0

Ryan M
Ryan M

Reputation: 20129

Unfortunately, there's not a good way to get a result from another Activity directly in a RecyclerView without involving the containing Activity/Fragment. Your best bet is probably to call startActivityForResult on the Activity or Fragment that contains the RecyclerView (you could pass a reference to it into your Adapter), and then pass the resulting information to your Adapter in the onActivityResult method.

Upvotes: 1

Related Questions