【发布时间】:2017-06-06 15:08:48
【问题描述】:
我有一个与 Stings 配合使用的应用程序,但我不知道如何将字符串从片段发送到活动。有人帮助我,但我不太了解代码。所以如果有人有问题,他可以问我,我可以回答它
请帮帮我
片段:
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;
}
主活动:
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" );
}
【问题讨论】:
标签: android android-activity fragment