0

The title almost says everything. I need suggestions on how can I do this. I might just add the outer adapter as a View on the AlertDialog to make it a little less complicated but I still don't know how can I interact with the AlertDialog from the inner adapter. I'm using RecyclerView.

This is how my data looks like.

"list_outer": [
  {
    "list_inner": [

    ]
  }
]

For example, if I select something from list_inner, I want to update the UI of the AlertDialog.

@Sreedev P R suggested that I use an interface that is to be passed on the adapters constructor. The problem is that there's another step before the AlertDialog can reach the inner adapter, currently, because I haven't added the outer adapter as a View yet. And if I do make the outer adapter just a View, I still can't think of a clean way to pass that value to the AlertDialog. Is it fine to reshow an AlertDialog? Would it not create a memory leak? I might just do that if that's fine.

This is how my app is structured.

Fragment
    |
    AlertDialog
        |
        Adapter outer
            |
            Adapter inner

Adapter inner needs to be able to communicate or change the UI of AlertDialog

This the layout for the AlertDialog.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
    android:focusable="true"
    android:id="@+id/lytDlgAddItemToTicketMain"
    android:focusableInTouchMode="true">

    <android.support.constraint.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <include
        android:id="@+id/include3"
        layout="@layout/dialog_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="@dimen/DialogScrollViewHeight"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline6"
        app:layout_constraintStart_toStartOf="@+id/guideline5"
        app:layout_constraintTop_toBottomOf="@+id/include3">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/lstAddItemToTicketAddOns"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:listitem="@layout/adptr_modifier">

            </android.support.v7.widget.RecyclerView>

        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>

This is it's code.

View v = getLayoutInflater().inflate(R.layout.dialog, null);

lst.setLayoutManager(new LinearLayoutManager(ctx));

AdapterOuter adptr = new ModifierAdapter(ctx, arrayList);
lst.setAdapter(adptr);

UPDATE

To make it a little complicated, I ditch the AlertDialog thing and made it a Fragment.

rminaj
  • 560
  • 6
  • 28
  • 1
    Use an interface to provide callback – Sreedev P R Jul 10 '19 at 06:29
  • Thanks for the advice. Which `class` should `implement` the `interface`? Is it the `Fragment` or the one that contains the `AlertDialog`? – rminaj Jul 10 '19 at 06:50
  • Now that I think about it more, it would be the inner adapter that needs to `implement` the `interface` right? How do I use it on the adapter? – rminaj Jul 10 '19 at 07:11
  • You might have to pass it in the adapter's constructor. – Sreedev P R Jul 10 '19 at 07:55
  • Like this? https://stackoverflow.com/a/15444411/10464730 And which adapter? – rminaj Jul 10 '19 at 08:01
  • Yes, like this. The adapter will be where you need to write function for alert dialog button action. – Sreedev P R Jul 10 '19 at 10:14
  • So, I do really need to make the outer adapter a `View` it seems. – rminaj Jul 11 '19 at 05:16
  • Could you give me an example of how I should do this? Like how do I setup my `Fragment`. Because that answer did show a snippet of the relevant parts of the `Fragment`. – rminaj Jul 11 '19 at 05:32
  • Hi, Can you explain further about your problem ?. You have a fragment which contains a recycler view and within that another recycler view ?, and what exactly do you want the callback to do ? – Sreedev P R Jul 11 '19 at 06:08
  • Also there is an other solution given in https://stackoverflow.com/a/15444411/10464730, that is to use an EventBus. I found it much easier than interfaces as interfaces might produce null pointer exceptions if not handled properly. – Sreedev P R Jul 11 '19 at 06:11
  • It's already all in the title. That's basically what I want to do. That link only got answer for `Adapter` to `Fragment` communication. The adapter is inside another adapter that is inside an `AlertDialog`. You get it? – rminaj Jul 11 '19 at 06:17
  • You can pass the interface as param to the outer adapter, and the same should be passed to the inner adapter. So whatever you interact with in the inner adapter will reach the Alert dialog – Sreedev P R Jul 11 '19 at 06:38
  • Oh. That might work. But how will the `AlertDialog` listen to that from the `Fragment`? – rminaj Jul 11 '19 at 06:40
  • Can you show me how you implemented the recyclerviews within alert dialog ? – Sreedev P R Jul 11 '19 at 06:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196303/discussion-between-sreedev-p-r-and-rminaj). – Sreedev P R Jul 11 '19 at 06:56

1 Answers1

0

After converting the AlertDialog into a Fragment and applying @Sreedev P R's suggestion of using an interface, I was able to make it work.

rminaj
  • 560
  • 6
  • 28