从片段到活动获取结果

时间:2016-04-27 09:19:27

标签: android android-fragments android-studio

这是片段

private TextView tv_name,tv_email,tv_message;
private SharedPreferences pref;
private AppCompatButton btn_change_password,btn_order,btn_logout;
private EditText et_old_password,et_new_password;
private AlertDialog dialog;
private ProgressBar progress;
EditText cardnumber;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_profile,container,false);
    initViews(view);
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    pref = getActivity().getPreferences(0);
    tv_name.setText("Welcome : "+pref.getString(Constants.NAME,""));
    tv_email.setText(pref.getString(Constants.EMAIL, ""));

}

private void initViews(View view){

    tv_name = (TextView)view.findViewById(R.id.tv_name);
    tv_email = (TextView)view.findViewById(R.id.tv_email);
    btn_change_password = (AppCompatButton)view.findViewById(R.id.btn_chg_password);
    btn_order = (AppCompatButton)view.findViewById(R.id.btn_order);
    btn_logout = (AppCompatButton)view.findViewById(R.id.btn_logout);
    btn_change_password.setOnClickListener(this);
    btn_order.setOnClickListener(this);
    btn_logout.setOnClickListener(this);

}

private void showDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog_change_password, null);
    et_old_password = (EditText)view.findViewById(R.id.et_old_password);
    et_new_password = (EditText)view.findViewById(R.id.et_new_password);
    tv_message = (TextView)view.findViewById(R.id.tv_message);
    progress = (ProgressBar)view.findViewById(R.id.progress);
    builder.setView(view);
    builder.setTitle("Change Password");
    builder.setPositiveButton("Change Password", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String old_password = et_old_password.getText().toString();
            String new_password = et_new_password.getText().toString();
            if (!old_password.isEmpty() && !new_password.isEmpty()) {

                progress.setVisibility(View.VISIBLE);
                changePasswordProcess(pref.getString(Constants.EMAIL, ""), old_password, new_password);

            } else {

                tv_message.setVisibility(View.VISIBLE);
                tv_message.setText("Fields are empty");
            }
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.btn_chg_password:
            showDialog();
            break;
        case R.id.btn_order:
            Intent myIntent = new Intent(getActivity(), activityresult1.class);
            startActivity(myIntent);
            break;
        case R.id.btn_logout:
            logout();
            break;
    }
}




private void logout() {
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean(Constants.IS_LOGGED_IN,false);
    editor.putString(Constants.EMAIL,"");
    editor.putString(Constants.NAME,"");
    editor.putString(Constants.UNIQUE_ID,"");
    editor.apply();
    goToLogin();
}

private void goToLogin(){

    Fragment login = new LoginFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame,login);
    ft.commit();
}

private void changePasswordProcess(String email,String old_password,String new_password){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    com.example.android.cardemulation.RequestInterface requestInterface = retrofit.create(com.example.android.cardemulation.RequestInterface.class);

    User user = new User();
    user.setEmail(email);
    user.setOld_password(old_password);
    user.setNew_password(new_password);
    ServerRequest request = new ServerRequest();
    request.setOperation(Constants.CHANGE_PASSWORD_OPERATION);
    request.setUser(user);
    Call<ServerResponse> response = requestInterface.operation(request);

    response.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

            ServerResponse resp = response.body();
            if(resp.getResult().equals(Constants.SUCCESS)){
                progress.setVisibility(View.GONE);
                tv_message.setVisibility(View.GONE);
                dialog.dismiss();
                Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

            }else {
                progress.setVisibility(View.GONE);
                tv_message.setVisibility(View.VISIBLE);
                tv_message.setText(resp.getMessage());

            }
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {

            Log.d(Constants.TAG,"failed");
            progress.setVisibility(View.GONE);
            tv_message.setVisibility(View.VISIBLE);
            tv_message.setText(t.getLocalizedMessage());


        }
    });
}

当我按下片段中的 btn_order 按钮时,我想进入 activityresult1 ,以便我可以转到 activityresult1 ,它会显示 tv_email 的EMAIL请帮助我

这是activityresult1

Button buttonorder;
TextView textviewemail;
private static final int REQUEST_CODE = 10;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activityresult1);

    Bundle extras = getIntent().getExtras();
    String tv_email = extras.getString("tv_email");
    textviewemail = (TextView) findViewById(R.id.textviewemail);
    textviewemail.setText("Welcome, " + tv_email+ " !" + "\nPlease select the food you want ! : ");

2 个答案:

答案 0 :(得分:1)

用于传递电子邮件值,请在订单按钮中单击

编写以下代码
Intent myIntent = new Intent(getActivity(), activityresult1.class);
myIntent.putExtra("email", pref.getString(Constants.EMAIL, ""));
startActivity(myIntent);

然后编写此代码以获取您想要的活动的电子邮件值

getIntent().getExtras().getString("email");

答案 1 :(得分:1)

在片段中实现以下界面:

1.76

在您的活动中实现CallbackFragment.Callback,覆盖此回调的方法。然后使用您要发送的数据。

实施如下:

 public interface Callback {
  //Here Object class means data which you want to sent from fragment to activity
   void setData(String email);
}