0

This answer helped a lot, but it seems that there's something still missing.

https://stackoverflow.com/a/56477713/11214643

The problem is that the 'action' method that sets the argument is telling me that my List is not the same type as the one defined in the navigation.xml, and if I try to write the xml argument as List, it just so happens that safeArgs does not support that type (weird if its the most common type managed by RecyclerViewAdapters), I also tried converting my List<> to an ArrayList<> and nothing.

enter image description here

My code:

<fragment
    android:id="@+id/product_list_fragment"
    android:name="com.example.myapp.ui.ProductListFragment"
    android:label="@string/product_list_title"
    tools:layout="@layout/fragment_product_list">
    <argument
        android:name="quote"
        app:argType="integer"
        android:defaultValue="0"
        />
    <argument
        android:name="productList"
        app:argType="com.example.myapp.data.pojos_entities.ProductQuantity[]"
        />

</fragment>


public class ProductQuantity {

    @Embedded
    public Quantity mQuantity_;

    @Relation(
            parentColumn = "child_product_id",
            entityColumn = "product_id"
    )
    public Product mProduct_;
}
Delark
  • 1,141
  • 2
  • 9
  • 15
  • 2
    From the error message, it mentions that it required an array instead of `List<>`. Try convert your List to array. – Joey Chong May 20 '20 at 02:21

1 Answers1

0

So I finally came with a solution and it looks like this:

enter image description here

But it is not as simple as that line. For the List<ProductQuantity> productQuantities, to be able to transform itself into a Parcelable Array[] (not an Array[], or an ArrayList, they are not the same) the Pojo (in this case ProductQuantity.class) MUST implement a Parcelable. like this:

enter image description here

Once you implement it, just press alt + Enter to implement all necessary methods.

BUT, That's not all.

The problem with having implemented methods in a ROOM Pojo used for relationship Queries is that it will not compile, so what you need to do next is to use the @Ignore interface on top of every method created by the Parcelable implementation.

Finally you will notice that it still does not compile, and for some reason, even tho you are Ignoring the constructor required by the Parcelable, you MUST create an EMPTY constructor for ROOM to be able to compile (even if a constructor was not necessary before it being a Parcelable)

The code:

public class ProductQuantity implements Parcelable {

    @Embedded
    public Quantity mQuantity_;

    @Relation(
            parentColumn = "child_product_id",
            entityColumn = "product_id"
    )
    public Product mProduct_;


    public ProductQuantity() {
/*Empty constructor required by ROOM*/
    }

    @Ignore
    protected ProductQuantity(Parcel in) {
    }

    @Ignore
    public static final Creator<ProductQuantity> CREATOR = new Creator<ProductQuantity>() {
        @Override
        public ProductQuantity createFromParcel(Parcel in) {
            return new ProductQuantity(in);
        }

        @Override
        public ProductQuantity[] newArray(int size) {
            return new ProductQuantity[size];
        }
    };


/*This methods don't need to be ignored for some reason*/
    @Override
    public int describeContents() {
       return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
    }
}

The conversion plus navigation:

            List<ProductQuantity> productQuantities = adapter.getCurrentList();

            ProductQuantity[] productQuantitiesArray = productQuantities
                    .toArray(new ProductQuantity[adapter.getItemCount()]);

            ProductsAndQuantitiesFragmentDirections.ActionProductsAndQuantitiesByQuoteFragmentToProductListFragment direction;

            Log.d(TAG, "onClick: quoteId is: " + quoteId);
            direction = ProductsAndQuantitiesFragmentDirections
                    .actionProductsAndQuantitiesByQuoteFragmentToProductListFragment(productQuantitiesArray).setQuote(quoteId);

            Navigation.findNavController(view).navigate(direction);

And one final tip to take notice is how the .toArray() method of the List class, DOES NOT include the rows or number of items of the List itself, that's why it is of upmost importance that you manually place the number of items in the code, in this case the adapter.getItemCount() method was used, but if you are working directly with a List<>, just use the .size() method of the List. in this particular case would be productQuantities.size()

enter image description here

Delark
  • 1,141
  • 2
  • 9
  • 15