2016-04-27 113 views
0

结果这片段获得从片段活动

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()); 


     } 
    }); 
} 

我想要得到activityresult1当我在片段按下按钮btn_order,这样我可以去activityresult1和它会显示电子邮件这是tv_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 ! : "); 
+0

比有什么问题,你可以通过意向从片段数据发送到活动 –

+0

你必须写一些听众。请参阅此链接:http://stackoverflow.com/questions/36832677/android-cant-update-textview-in-fragment-from-activity-nullpointerexception/36833137#36833137 –

+0

@MohitSuthar我不能发送它 – Vyshunavi

回答

1

传递的电子邮件值写在命令按钮下面的代码点击

Intent myIntent = new Intent(getActivity(), activityresult1.class); 
myIntent.putExtra("email", pref.getString(Constants.EMAIL, "")); 
startActivity(myIntent); 

然后编写此代码为得到你想要

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

第二部分不能突出显示红色 – Vyshunavi

+0

它仍然不能正常工作 – Vyshunavi

+0

你是否收到任何错误信息? – Masum

1

中实现以下接口,其活动的电子邮件值你的片段:

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

implements Call在您的活动中backFragment.Callback,覆盖此回调的方法。然后使用您发送的数据。

实现象下面这样:

public class MainCallbackActivity extends Activity implements CallbackFragment.Callback { 

@Override 
public void setData(String email) { 
    //use this email in local (i.e.,in activity class) 
    this.email = email; 
    } 
} 
+0

感谢您的帮助! – Vyshunavi

+0

你也是女孩吗? – Vyshunavi

+0

Yah..hey你想发送电子邮件到片段添加的活动r你想发送给其他活动? –