我是Java新手。活动是接收ID并重用该ID以使用Recipe API获取配方的详细信息。我使用三个选项卡来显示不同的内容。我无法弄清楚的问题是第一个(描述选项卡)和第三个(步骤选项卡)可以从主活动接收字符串,但第二个选项卡(成分选项卡)始终从主活动接收空值。此外,当我运行活动时,字符串不会立即显示在第一个选项卡上。它仅在我点击第三个选项卡并返回第一个选项卡后显示。
活动
public class SearchHomeResultActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public String RecipeId = "479101";
public String SInstruction;
public String SIngredients;
public String STitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
new CallMashapeAsync().execute(RecipeId);
}
public class CallMashapeAsync extends AsyncTask<String, Integer, HttpResponse<JsonNode>> {
protected HttpResponse<JsonNode> doInBackground(String... msg) {
HttpResponse<JsonNode> request = null;
try {
request = Unirest.get("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/" + msg[0] + "/information")
.header("X-Mashape-Key", "EulyPgSat2mshhTi8JJxY40UEWzdp1mMmEGjsnrbQq1AB0vuOY")
.header("X-Mashape-Host", "spoonacular-recipe-food-nutrition-v1.p.mashape.com")
.asJson();
} catch (UnirestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return request;
}
protected void onProgressUpdate(Integer... integers) {
}
protected void onPostExecute(HttpResponse<JsonNode> response) {
String answer = response.getBody().toString();
try {//get all response
JSONObject obj = new JSONObject(answer);
//get ingredients
String Ringredient = obj.getString("extendedIngredients");
List<String> terms1 = new ArrayList<String>();
JSONArray ing = new JSONArray(Ringredient);
for (int i = 0; i < ing.length(); i++) {
JSONObject ING1 = ing.getJSONObject(i);
String Ostr = ING1.getString("originalString");
terms1.add(Ostr);
}
String listString = "";
for (String s : terms1) {
listString += s + "\n";
}
//get instructions
String Rid1 = obj.getString("instructions");
//get title
String Rid2 = obj.getString("title");
SInstruction = Rid1;
SIngredients = listString;
STitle = Rid2;
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
public String getIns() {
//
return SInstruction;
}
public String getIngredients() {
//
return SIngredients;
}
public String getRTitle() {
//
return STitle;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
descriptionTab tab1 = new descriptionTab();
return tab1;
case 1:
IngredientsTabActivity tab2 = new IngredientsTabActivity();
return tab2;
case 2:
StepTabActivity tab3 = new StepTabActivity();
return tab3;
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
片段
public class StepTabActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.step_layout, container, false);
SearchHomeResultActivity getData = (SearchHomeResultActivity) getActivity();
String Rid = getData.getIngredients();
TextView txt = rootView.findViewById(R.id.name);
txt.setText(Rid);
return rootView;
}
这是Step(第三个)片段的代码。其他两个是相同的,但只是调用不同的方法来获取TextView的不同字符串。 请帮我。非常感谢你!
答案 0 :(得分:0)
您可以在片段中创建一个名为newInstance的构造函数,如下所示,
public static FragmentName newInstance(String str) {
Bundle args = new Bundle();
FragmentName fragment = new FragmentName();
fragment.setArguments(args);
resID = str;
return fragment;
}
将您的“resID”定义为全局变量,并在fragment的onCreate方法中使用它。
并且当你向viewPager适配器添加片段时,从活动开始就像这样
adapter.addFrag(FragmentName.newInstance(resID), "fragment_name");