2017-06-06 5 views
-2

Stingsで動作するアプリケーションがありますが、フラグメントをアクティビティに送信する方法はわかりません。誰かが私を助けるが、私は誰かが疑問を持っている場合は、彼が私を求めることができると私はそれがフラグメントからアクティビティへスティッキングを取得する方法

くれ

断片助けてくださいanserdできwell.Soコードを理解しない:

public class Fragment_1 extends Fragment { 



    String text; 
    int zähler = 0; 
    String teile[]; 
    String in = "", in2 = "", in3 = ""; 
    ListView listView; 
    public String [] liste; 
    String value = "MIT"; 



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

     Bundle bundle = new Bundle(); 
     bundle.putString("Key",value); 

     Intent intent = getActivity().getIntent(); 
     intent.putExtras(bundle); 

     View view = inflater.inflate(R.layout.fragment_1, container, false); 

     listView = (ListView) view.findViewById(R.id.listView); 
     // final Button but = (Button) view.findViewById(R.id.but1) ; 





     new doit().execute(); 



     return view; 
    } 

MainActivity:

public class MainActivity extends AppCompatActivity { 
    String mesg = "MIT"; 





    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    private SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 

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



     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 


     Bundle data = getIntent().getExtras(); 
     if (data != null) { 

      String receivedString = data.getString("Key"); 

      Log.d("MainActivity", receivedString); 
     } 





     Log.d("MainActivity", "Bitte nicht"); 



    } 

答えて

0

フラグメント内にインターフェイスを使用する必要があります。

public class MyFragment extends Fragment 
{ 

private MyInterface mListener; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    return inflater.inflate(R.layout.mylayout, container, false); 
} 

@Override 
public void onActivityCreated(Bundle bundle) 
{ 
    super.onActivityCreated(bundle); 
    mListener.passString("Hello from fragment!"); 
} 

@Override 
public void onAttach(Context context) 
{ 
    super.onAttach(context); 
    if (context instanceof MyFragment.MyInterface) 
    { 
     mListener = (MyInterface) context; 
    } 
    else 
    { 
     throw new RuntimeException(context.toString()); 
    } 
} 

@Override 
public void onDetach() 
{ 
    super.onDetach(); 
    mListener = null; 
} 

public interface MyInterface 
{ 
    void passString(String string); 
} 

} 

そして、あなたの活動には、インターフェイスのメソッドを実装する必要があります。

public class MyActivity extends AppCompatActivity implements MyFragment.MyInterface 
{ 
String myString; 
//other methods like onCreate and so on... 

@Override 
public void passString(String string) 
{ 
    myString = string; 
} 

} 

のmyStringは、あなたが活動にフラグメントから渡された文字列になります。ここで

0

あなたは、単にあなたの活動に戻ってあなたのフラグメントとの間でデータを転送するためにバンドルを使用している:あなたが活動にフラグメントから戻って行くのか

//sending data from your fragment 
Bundle bundle = new Bundle(); 
bundle.putString("KEY",value); 

Intent intent = getActivity().getIntent(); 
intent.putExtras(bundle); 


//Receving data inside your Activity 
Bundle data = getIntent().getExtras(); 
if (data != null) 
     { 
      String receivedString= data.getString("KEY"); 

     } 
+0

データは常に私 – Curio

+0

の場合はnullです?? – sumit

+0

あなたが書いたとおりです。フラグメントの最初の部分とアクティビティの2番目の部分 – Curio

関連する問題