Java 在同一活动的2个片段之间发送ArrayList

Java 在同一活动的2个片段之间发送ArrayList,java,android,listview,android-fragments,arraylist,Java,Android,Listview,Android Fragments,Arraylist,我有两个片段和一个活动。 片段1有一个带有listview和一些按钮等的相对布局, 片段2在底部有一个ListView和一个按钮。fragment 2的listview是一个listview,您可以在其中选择多个项目,当一个项目被选中时,它将被添加到arraylist(我们将称之为selected)中,或者如果取消选中,它将被删除。我想将此arraylist发送到fragment 1,它可以在其中将其添加到自己的listview(以及其他项目,如果它们存在的话)。我已将选定的ArrayList存

我有两个片段和一个活动。 片段1有一个带有listview和一些按钮等的相对布局, 片段2在底部有一个ListView和一个按钮。fragment 2的listview是一个listview,您可以在其中选择多个项目,当一个项目被选中时,它将被添加到arraylist(我们将称之为selected)中,或者如果取消选中,它将被删除。我想将此arraylist发送到fragment 1,它可以在其中将其添加到自己的listview(以及其他项目,如果它们存在的话)。我已将选定的ArrayList存储在“活动”中

活动具有以下代码

public class AddExerciseActivity extends AppCompatActivity implements ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;
static ArrayList<Set> sets = new ArrayList<>();

String name, date;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        name = extras.getString("ExerciseName");
        date = extras.getString("date");
    }

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setTitle(name);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}




/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        switch (position) {
            case 0:
                Bundle bundle = new Bundle();
                bundle.putString("date", date);
                bundle.putInt("position", position);
                return new FirstFragment();
            case 1:
                return new SecondFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }



    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Track";
            case 1:
                return "History";
        }
        return null;
    }
}

public ArrayList<Set> getSets() {
    return sets;
}

public void setSets(ArrayList<Set> sets) {
    this.sets = sets;
}
}

这可能有用,但我还没有测试过。 看起来您可以使用
onTabSelected()
方法中的新方法调用来传递
捆绑包

创建对
FirstFragment
的引用,作为
AddExerciseActivity
中的实例变量:

ViewPager mViewPager;
static ArrayList<Set> sets = new ArrayList<>();
FirstFragment fragmentFirst; //added
然后,添加,将
集合
添加到
,并将其传递到
中的
第一个片段

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());

    if (fragmentFirst != null){
       Bundle bundle = new Bundle();
       bundle.putSerializable("sets", sets);
       //fragmentFirst.setArguments(bundle); //not working
       fragmentFirst.updateArguments(bundle); //try this
    }

}
编辑:在
FragmentFirst
中添加新的公共方法:

public void updateArguments(Bundle args) { 
     ArrayList<Set> x = new ArrayList<>(); 
     x = (ArrayList<Set>) args.getSerializable("sets"); 
     for(Set p: x) { 
        array.add(p); 
     } 
     mAddExerciseAdapter.notifyDataSetChanged(); 
   } 
public void updateArguments(Bundle args){
ArrayList x=新的ArrayList();
x=(ArrayList)args.getSerializable(“集合”);
对于(集p:x){
数组。添加(p);
} 
mAddExerciseAdapter.notifyDataSetChanged();
} 
1)将这两个片段存储在主活动中的公共类变量中。
2) 使用onAttach(活动a)重载在两个片段中保存活动。
3) 在两个片段中生成一个函数,该函数都采用arrayList。

4) 通过从onAttach保存的mainactivity调用另一个片段中的函数。

为什么不将列表保留在活动中看一看它可能是@bharat的复制品,我试着实现它,但它不起作用,@BojanKseneman你是什么意思?哪一个??array=args.getSerializable(“集合”)//必需的java.lang.array,找到java.io.serializable我有一个名为Card的类,它是可序列化的,里面有一个ArrayList的“Set”,传递这个overTry
array=(ArrayList)args.getSerializable(“sets”)
可能会更容易些,看看它是否有效。它产生了一个错误,我会用它的产品完美地更新我的问题,虽然我对updateArguments做了一些更改,因为它没有做任何事情,但public void updateArguments(Bundle args){ArrayList x=new ArrayList();x=(ArrayList)args.getSerializable(“Set”);for(Set p:x){array.add(p);}mAddExerciseAdapter.notifyDataSetChanged();}
ViewPager mViewPager;
static ArrayList<Set> sets = new ArrayList<>();
FirstFragment fragmentFirst; //added
@Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        switch (position) {
            case 0:
                Bundle bundle = new Bundle();
                bundle.putString("date", date);
                bundle.putInt("position", position);
                fragmentFirst = new FirstFragment(); //modified
                return fragmentFirst; //added
            case 1:
                return new SecondFragment();
        }
        return null;
    }
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());

    if (fragmentFirst != null){
       Bundle bundle = new Bundle();
       bundle.putSerializable("sets", sets);
       //fragmentFirst.setArguments(bundle); //not working
       fragmentFirst.updateArguments(bundle); //try this
    }

}
public void updateArguments(Bundle args) { 
     ArrayList<Set> x = new ArrayList<>(); 
     x = (ArrayList<Set>) args.getSerializable("sets"); 
     for(Set p: x) { 
        array.add(p); 
     } 
     mAddExerciseAdapter.notifyDataSetChanged(); 
   }