2017-01-02 97 views
2

我是Android開發中的新成員。我試圖從片段值傳遞給活動,但這麼多的嘗試後,我無法得到答案......如何將片段中的值傳遞給活動

這是我的片段:

public OtpGentation(int OTP) 
{ 
    this.OTP =OTP; 
} 
public OtpGentation(String number1, String email1, String password) 
{ 
    number = number1; 
    mail = email1; 
    pass = password; 
} 

public OtpGentation() { 


} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    rootview = inflater.inflate(R.layout.otp, container, false); 
    Bundle bundle = getArguments(); 

    new OTPAsyncManager().execute(); 

    etxotp =(EditText)rootview.findViewById(R.id.etxotp); 
    btnNext = (Button) rootview.findViewById(R.id.nextToOTP); 
    btnCancel =(Button) rootview.findViewById(R.id.cancel); 

    //etxotp.setText(OTP); 
    btnNext.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) 
     { 

      if (etxotp.getText().toString().equals("")) 
      { 
       Toast.makeText(getContext(), "Please Enter OTP ", Toast.LENGTH_SHORT).show(); 

      } 
      else 
      { 
       int enteredOtp = Integer.parseInt(etxotp.getText().toString()); 
       if (enteredOtp ==OTP) 
       { 

        Toast.makeText(getContext(), "OTP Matched", Toast.LENGTH_SHORT).show(); 
        Bundle bun = new Bundle(); 
        bun.putString("no",number); 
        bun.putString("ma",mail); 
        bun.putString("pa",pass); 
        Intent subintent = new Intent(getContext(),SubmitRegistration.class); 
        subintent.putExtras(bun); 
        startActivity(subintent); 



       } 
       else 
       { 
        Toast.makeText(getContext(), "Please Enter Correct OTP ", Toast.LENGTH_SHORT).show(); 
       } 

      } 


     } 
    }); 
    btnCancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) 
     { 

      fragmentManager = getActivity().getSupportFragmentManager(); 
      HomeScreen fragmentOne = new HomeScreen(); 

      fragmentManager 
        .beginTransaction() 
        .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right) 
        .replace(R.id.content_frame, fragmentOne) 
        .addToBackStack("") 
        .commit(); 

     } 
    }); 


    return rootview; 
} 

而這正是我想要的類通過數值

+0

它看起來像你的問題是不完整 – Manza

+0

是代碼給你一個錯誤,如果是的話顯示我們 –

回答

0

這是我使用的解決方案,這工作,但它不是一個好的做法

內,您的活動寫getter和setter方法要傳遞的數據,例如,如果你想傳遞一個字符串名稱寫

String fileName; 

public String getFileName() { 
    return fileName; 
} 
public void setFileName(String fileName) { 
    this.fileName = fileName; 
} 
在片段

public class YourFragment extends Fragment { 

    Context contextCheckClass; 
    private String fileName; 



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

    View group=(View)inflater.inflate(R.layout.fragment_highlight, null); 

     fileName=((MainActivity) getActivity()).setFileName("Your String to pass to activity"); //set what ever string you want to pass 

    return group; 
} 

} 

如果你這樣做的方式,你不能在其他任何地方使用這個片段,如果要訪問不同的活動相同片斷,然後

MainActivity

在你的片段中創建一個構造函數傳遞上下文。你會注意到它會給出一個關於避免非默認構造函數忽略它或禁止檢查的警告

public class YourFragment extends Fragment { 

    private String fileName; 
    Context contextCheckClass; 

    public YourFragment (Context ctx) { 
     this.contextCheckClass=ctx; 
    } 

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

    View group=(View)inflater.inflate(R.layout.fragment_highlight, null); 
    if(contextCheckClass instanceof MainActivity){ //write your Activity name here 
     //This fragment is called from MainActivity 
     fileName=((MainActivity) getActivity()).getFileName(); 

     } 
     else { 
     //This fragment is called from some other activity 
     } 

    return group; 
} 

加載該片段通過活動情境它

YourFragment yourFragment =new YourFragment (MainActivity.this); 
getSupportFragmentManager() 
.beginTransaction() 
.add(R.id.LL_Fragment, yourFragment) 
.addToBackStack(null) 
.commit(); 
2

你應該在你的片段創建一個接口,你應該實現的接口到你的Activity類。

例如,在您的片段:

OnHeadlineSelectedListener mCallback; 

    // Container Activity must implement this interface 
    public interface OnHeadlineSelectedListener { 
     public void onArticleSelected(int position); 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (OnHeadlineSelectedListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnHeadlineSelectedListener"); 
     } 
    } 

您可以發送任何值,像這樣:

 // Send the event to the host activity 
     mCallback.onArticleSelected(position); 

而且你的活動應該是這樣的:

public static class MainActivity extends Activity 
     implements HeadlinesFragment.OnHeadlineSelectedListener{ 
    ... 

    public void onArticleSelected(int position) { 
     // The user selected the headline of an article from the HeadlinesFragment 
     // Do something here to display that article 
    } 
} 

更多您可以檢查的信息the offical documentation

+0

onAttach(活動活動):現在不推薦使用。 –

+1

是的,onAttach(上下文上下文)更有意義。 – ziLk

相關問題