在View Pager之间获取价值

时间:2018-04-04 10:34:42

标签: java android fragment

我想从我的actvity ControlAccountPassword.java获取我的FragmentEditoreProfile.java的值,我正在使用视图分页器和标签布局,我可以使用什么来从我的活动传输数据来查看像Intent等的寻呼机片段。 这是我的代码 ControlAccountPassword.java

    public class ControlAccountPassword extends AppCompatActivity {
    private TabLayout tab_Layout;
    private AppBarLayout appBarLayout;
    private ViewPager view_Pager;
    private ImageView img1;
    private String temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_p_change_p);
        //Get Intent Data
        Intent intent=getIntent();
        temp=intent.getStringExtra("email");
        tab_Layout=(TabLayout)findViewById(R.id.tabLayout);
        appBarLayout=(AppBarLayout)findViewById(R.id.appBar);
        view_Pager=(ViewPager)findViewById(R.id.viewPager);
        img1=(ImageView)findViewById(R.id.imgBack);
        //adding fragment
        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
        adapter.AddFragment(new FragmentEditProfile(),"Edit Profile");
        adapter.AddFragment(new FragmentChangePassword(),"Change Password");
        //adapter setting
        view_Pager.setAdapter(adapter);
        tab_Layout.setupWithViewPager(view_Pager);
        //onClickListener
        onClickBack(this);
    }
    public void onClickBack(ControlAccountPassword view){
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ControlAccountPassword.super.onBackPressed();
                System.exit(1);
            }
        });
    }
}

FragmentEditorProfile.java

public class FragmentEditProfile extends Fragment {
View view;
private ProgressDialog progress;
private String URL;
private String name,phone,email;
private GetURL getURL=new GetURL(URL);

public FragmentEditProfile() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view=inflater.inflate(R.layout.edit_profile,container,false);
    return view;
}
public void getDataPelanggan(){
    progress = new ProgressDialog(getActivity());
    progress.setCancelable(false);
    progress.setMessage("Loading. . .");
    progress.show();


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(getURL.GetMyURL())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    PelangganAPI api = retrofit.create(PelangganAPI.class);

    //Call<PelangganList> call = api.getPelanggan();
}}

1 个答案:

答案 0 :(得分:0)

创建新类并扩展Application:

public class YourClassName extends Application {

    public Context context;
    public static int iID;
    public static String sDeviceID;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
}

您可以随处更改公共静态var的值,或者在任何地方使用变量:

在其他课程中:

YourClassName.sDeviceID = "EB67";

在另一个班级:

int iVar;
String sVar;
iVar = YourClassName.iID;
sVar = YourClassName.sDeviceID;