Android 从第二个片段更新一个片段中的ListView
我有一个Android 从第二个片段更新一个片段中的ListView,android,listview,android-listview,android-fragments,android-viewpager,Android,Listview,Android Listview,Android Fragments,Android Viewpager,我有一个ViewPager,显示用户2的片段 片段1包含一个带有onClickListener的按钮,该按钮向片段2中的ListView内容添加一个条目。新条目是由随机数生成器生成的数字 片段2包含一个列表视图,其中包含一些随机数 我找不到从片段1中的按钮侦听器刷新片段2中的列表的方法我该怎么做? 我试过: 在中的onResume中初始化和设置列表适配器 片段2 从片段1调用myViewPager.notifyDataSetChanged 上述两种方法都不起作用。有什么想法吗 编辑:这是我
ViewPager
,显示用户2的片段
- 片段1包含一个带有
的按钮,该按钮向片段2中的onClickListener
内容添加一个条目。新条目是由随机数生成器生成的数字ListView
- 片段2包含一个
,其中包含一些随机数列表视图
onResume
中初始化和设置列表适配器
片段2编辑:这是我的代码: layout\u main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/main_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add Number" />
</RelativeLayout>
您可能需要实现其中一些功能,以使其正常工作: 还有更多的阅读:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
public class SlideActivity extends FragmentActivity {
private final static int FRAGMENT_NUMBER = 2;
private ViewPager pager;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_mainpager);
pager = (ViewPager) findViewById(R.id.mainpager);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
super.onCreate(savedInstanceState);
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int index) {
switch(index){
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return new Fragment1();
}
}
public int getCount() {
return FRAGMENT_NUMBER;
}
}
}