-1

I have a MainActivity and a fragment. What I want to do is pass array list from my main activity to my fragment.

In my MainActivity my code looks like this:

private final List<RestaurantParcelableModel> restaurantList = new ArrayList<>();
...
RestaurantsFragment restaurantsFragment = new RestaurantsFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList("restaurantList", (ArrayList<? extends Parcelable>) restaurantList);
        restaurantsFragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.restaurants_list, restaurantsFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

Right now I am getting this error in fragmentTransaction.replace(R.id.restaurants_list, restaurantsFragment); - "Wrong 2nd argument type. Found: 'com.test.restaurants.sliderfragments.RestaurantsFragment', required: 'android.app.Fragment' replace (int, android.app.Fragment) in FragmentTransaction cannot be applied to (int, com.test.restaurants.sliderfragments.RestaurantsFragment)"

Banana
  • 2,435
  • 7
  • 34
  • 60

6 Answers6

2

@Kemo the same error i was facing now I used fragment support manager instead of fragment manager.Now its working fine.

        FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = 
        fragmentManager.beginTransaction();
        BlankFragment blankFragment = new BlankFragment();
        fragmentTransaction.add(R.id.ll, blankFragment, "");
        fragmentTransaction.commit();
2

It's because your class RestaurantParcelableModel does not implements the Parceable class. Implement parceable class and override the required methods.

Visit: http://www.vogella.com/tutorials/AndroidParcelable/article.html

Mayank Raj
  • 921
  • 2
  • 12
  • 23
1

You can use Bundle to send your ArrayList:

Bundle args = new Bundle();
args.putStringArrayList("array", mArrayList);
restaurantsFragment.setArguments(args);
user1506104
  • 6,554
  • 4
  • 71
  • 89
1

You have to delete line: import android.support.v4.app.Fragment; and change it for import android.app.Fragment;

W.Kurek
  • 299
  • 2
  • 11
1

Now to fix the issue you have to handle your imports.

Either use import android.app.Fragment instead of import android.support.v4.app.Fragment

Or Update the main content of your app with a support Fragment manager like this:

FragmentManager fragmentManager = getSupportFragmentManager(). 

But then you also have to change the type of the FragmentManager itself; import android.app.FragmentManager to import android.support.v4.app.FragmentManager;

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
0

You can use parceable or serializabale to pass the object of arraylist or array list.You can find the example of parceable from the givenlink of github. ParceableSample.zip download the zip file

You can use the above link sample for passing the Array list or Object of array list.