简体   繁体   English

如何在Android中将ArrayList从Activity传递到Fragment

[英]How to Pass ArrayList from Activity to Fragment in Android

I have a MainActivity and a fragment. 我有一个MainActivity和一个片段。 What I want to do is pass array list from my main activity to my fragment. 我要做的是将数组列表从我的主要活动传递到片段。

In my MainActivity my code looks like this: 在我的MainActivity中,我的代码如下所示:

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 ); 现在,我在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) " -“ 错误的第二个参数类型。找到:'com.test.restaurants.sliderfragments.RestaurantsFragment',必需:'android.app.Fragment'替换FragmentTransaction中的(int,android.app.Fragment)不能应用于(int,com .test.restaurants.sliderfragments.RestaurantsFragment)

@Kemo the same error i was facing now I used fragment support manager instead of fragment manager.Now its working fine. @Kemo我现在遇到的相同错误是我使用片段支持管理器而不是片段管理器。

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

It's because your class RestaurantParcelableModel does not implements the Parceable class. 这是因为您的类RestaurantParcelableModel没有实现Parceable类。 Implement parceable class and override the required methods. 实现可分割的类并覆盖所需的方法。

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

You can use Bundle to send your ArrayList: 您可以使用Bundle发送ArrayList:

Bundle args = new Bundle();
args.putStringArrayList("array", mArrayList);
restaurantsFragment.setArguments(args);

You have to delete line: import android.support.v4.app.Fragment; 您必须删除以下行: import android.support.v4.app.Fragment; and change it for import android.app.Fragment; 并将其更改为import android.app.Fragment;

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 使用import android.app.Fragment而不是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; 但是然后,您还必须更改FragmentManager本身的类型。 import android.app.FragmentManager to import android.support.v4.app.FragmentManager; import android.app.FragmentManagerimport android.support.v4.app.FragmentManager;

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. 您可以使用parceable或serializabale传递arraylist或array list的对象。您可以从github的给定链接中找到parceable的示例。 ParceableSample.zip download the zip file ParceableSample.zip下载zip文件

You can use the above link sample for passing the Array list or Object of array list. 您可以使用上面的链接示例来传递数组列表或数组列表的对象。

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

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