2017-01-02 43 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

を渡すために、これは動作しますが、そのない良い練習あなたが渡したいデータのためのあなたの活動の書き込みゲッターとセッター内部

文字列名の書き込み

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

関連する問題