0

片段1的另一片段:仅包含EDITTEXT FRAGMENT 2:只包含EDITTEXT 片段3:含按钮及EDITTEXT如何值可以从一个片段被传递到viewpager

片段是鉴于寻呼机 所以,我想通过片段1和fragment2的值,例如,当我在第三个片段的按钮,点击 从EditText上(片段1和fragment2)所有值database.i获得进入已经保持数据库

需要用简单的例子帮助...在此先感谢

+0

您可以使用SharedPreferences。 – statosdotcom

+1

定义一个接口到你的活动片段 https://developer.android.com/training/basics/fragments/communicating.html – mariozawa

+0

看看这个http://stackoverflow.com/questions/29731038/passing -data-beetwen-fragments-in-viewpager –

回答

0

您可以使用ViewPager.OnPageChangeListener onPageSelected方法。

在你的片段添加一个公共方法是这样的:

private Object myObject; 

public void addObject(Object sampleObject){ 
    myObject = sampleObject; 
} 

然后使用onPageSelected方法,你可以用这个试试吧:

@Override 
public void onPageSelected(int position) { 
    currentFragment = (Fragment) viewPagerAdapter.instantiateItem(mViewPager, position); 

    if (currentFragment instanceof yourFragment) { 

     ((yourFragment) currentFragment).addObject(objectToBePassed); 
    } 

} 
0

我不知道,但尝试此方法... 使用包从一个片段发送到另一个字符串

//Put the value 
YourNewFragment ldf = new YourNewFragment(); 
Bundle args = new Bundle(); 
args.putString("YourKey", "YourValue"); 
ldf.setArguments(args); 

//Inflate the fragment 
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit(); 

写重码接受片段createView()

//Retrieve the value 
String value = getArguments().getString("YourKey"); 
相关问题