我有一个包含3个片段的分页器(活动B中有片段),并且在第一次单击“回收者视图”项目时(在具有回收者视图的活动A中),我在加载数据时遇到了问题,这3个片段被加载为再次单击“回收者”视图项时应如此。
**活动A->活动B(不加载首次点击数据)**活动B->活动A(按下后退按钮)**活动A->活动B(此次加载)
回收者视图项目是从JSON加载的。
即使在第一种情况下,我也可以在logcat中跟踪特定单击的回收站视图的JSON。
发现另一个奇怪的事情,单击的下一个“回收者视图”项将加载上一个单击的项,而不是活动B中的当前单击的项。
public class Recipe_Details extends AppCompatActivity implements
InfredientsTab.OnFragmentInteractionListener,
FragmentNutrition.OnFragmentInteractionListener,WebsiteRefered.OnFragmentInteractionListener {
TabLayout tabLayout;
TextView textView;
ImageView imageView;
static String websiteurl,ingredients,energy,fat,sugar,carbs,protein,cholestrol,sodium,calcium,mangnesium,
iron,zinc,vitaminA,vitaminC,
vitaminB6,vitaminD,vitaminB12,vitaminK,calorie,carblevel,weight;
public static String getWeight() {
return weight;
}
public static void setWeight(String weight) {
Recipe_Details.weight = weight;
}
public static String getCalorie() {
return calorie;
}
public static void setCalorie(String calorie) {
Recipe_Details.calorie = calorie;
}
public static String getCarblevel() {
return carblevel;
}
public static void setCarblevel(String carblevel) {
Recipe_Details.carblevel = carblevel;
}
public static String getVitaminK() {
return vitaminK;
}
public static void setVitaminK(String vitaminK) {
Recipe_Details.vitaminK = vitaminK;
}
public static String getVitaminB12() {
return vitaminB12;
}
public static void setVitaminB12(String vitaminB12) {
Recipe_Details.vitaminB12 = vitaminB12;
}
public static String getIron() {
return iron;
}
public static void setIron(String iron) {
Recipe_Details.iron = iron;
}
public static String getSugar() {
return sugar;
}
public static void setSugar(String sugar) {
Recipe_Details.sugar = sugar;
}
public static String getEnergy() {
return energy;
}
public static void setEnergy(String energy) {
Recipe_Details.energy = energy;
}
public static String getWebsiteurl() {
return websiteurl;
}
public static void setWebsiteurl(String websiteurl) {
Recipe_Details.websiteurl = websiteurl;
}
public static String getIngredients() {
return ingredients;
}
public static void setIngredients(String ingredients) {
Recipe_Details.ingredients = ingredients;
}
public static String getFat() {
return fat;
}
public static void setFat(String fat) {
Recipe_Details.fat = fat;
}
public static String getCarbs() {
return carbs;
}
public static void setCarbs(String carbs) {
Recipe_Details.carbs = carbs;
}
public static String getProtein() {
return protein;
}
public static void setProtein(String protein) {
Recipe_Details.protein = protein;
}
public static String getCholestrol() {
return cholestrol;
}
public static void setCholestrol(String cholestrol) {
Recipe_Details.cholestrol = cholestrol;
}
public static String getSodium() {
return sodium;
}
public static void setSodium(String sodium) {
Recipe_Details.sodium = sodium;
}
public static String getCalcium() {
return calcium;
}
public static void setCalcium(String calcium) {
Recipe_Details.calcium = calcium;
}
public static String getMangnesium() {
return mangnesium;
}
public static void setMangnesium(String mangnesium) {
Recipe_Details.mangnesium = mangnesium;
}
public static String getZinc() {
return zinc;
}
public static void setZinc(String zinc) {
Recipe_Details.zinc = zinc;
}
public static String getVitaminA() {
return vitaminA;
}
public static void setVitaminA(String vitaminA) {
Recipe_Details.vitaminA = vitaminA;
}
public static String getVitaminC() {
return vitaminC;
}
public static void setVitaminC(String vitaminC) {
Recipe_Details.vitaminC = vitaminC;
}
public static String getVitaminB6() {
return vitaminB6;
}
public static void setVitaminB6(String vitaminB6) {
Recipe_Details.vitaminB6 = vitaminB6;
}
public static String getVitaminD() {
return vitaminD;
}
public static void setVitaminD(String vitaminD) {
Recipe_Details.vitaminD = vitaminD;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_details);
textView = findViewById(R.id.recipe_name_id);
imageView = findViewById(R.id.image_id);
String image_url = getIntent().getExtras().getString("imageurl");
String name = getIntent().getExtras().getString("name");
String lim0 = getIntent().getExtras().getString("lim0");
String lim1 = getIntent().getExtras().getString("lim1");
final String pos = getIntent().getExtras().getString("pos");
String url = "https://api.edamam.com/search?q="+name
+"&app_id="+APIKEY.getApiId()+"&app_key="+APIKEY.getAPI_key()+"&from="+lim0+"&to="+lim1;
StringRequest stringRequest = new StringRequest(Request.Method.GET,url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.i("hello",response);
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("hits");
for (int i = 0; i < jsonArray.length(); i++) {
if(i==(Integer.parseInt(pos)%10)){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONObject jsonObject2 = jsonObject1.getJSONObject("recipe");
String url = jsonObject2.getString("url");
setWebsiteurl(url);
JSONArray jsonArray1 = jsonObject2.getJSONArray("ingredientLines");
StringBuilder ingredients = new StringBuilder();
for (int j=0;j<jsonArray1.length();j++) {
ingredients = ingredients.append(jsonArray1.getString(j));
ingredients.append("\n\n");
}
setIngredients(ingredients.toString());
String quantity = "quantity";
String g = " g";
String kcal = " kcal";
String mg = " mg";
String ug = " ug";
String name;
JSONArray jsonArray2 = jsonObject2.getJSONArray("dietLabels");
StringBuilder carblevel = new StringBuilder();
for (int j=0;j<jsonArray2.length();j++) {
carblevel = carblevel.append(jsonArray2.getString(j));
carblevel.append("\n");
}
setCarblevel(carblevel.toString());
String calories = jsonObject2.getString("calories");
String[] split = calories.split("\\.");
setCalorie(split[0]);
String weight = jsonObject2.getString("totalWeight");
setWeight(weight.substring(0,6).concat(" g"));
JSONObject jsonObject3 = jsonObject2.getJSONObject("totalNutrients");
JSONObject jsonObject4 = jsonObject3.getJSONObject("ENERC_KCAL");
name = jsonObject4.getString(quantity);
setEnergy(name.substring(0,4).concat(kcal));
JSONObject jsonObject5 = jsonObject3.getJSONObject("FAT");
name = jsonObject5.getString(quantity);
setFat(name.substring(0,4).concat(g));
JSONObject jsonObject6 = jsonObject3.getJSONObject("CHOCDF");
name = jsonObject6.getString(quantity);
setCarbs(name.substring(0,4).concat(g));
JSONObject jsonObject7 = jsonObject3.getJSONObject("SUGAR");
name = jsonObject7.getString(quantity);
setSugar(name.substring(0,4).concat(g));
JSONObject jsonObject8 = jsonObject3.getJSONObject("PROCNT");
name = jsonObject8.getString(quantity);
setProtein(name.substring(0,4).concat(g));
JSONObject jsonObject9 = jsonObject3.getJSONObject("CHOLE");
name = jsonObject9.getString(quantity);
setCholestrol(name.substring(0,4).concat(mg));
JSONObject jsonObject10 = jsonObject3.getJSONObject("NA");
name = jsonObject10.getString(quantity);
setSodium(name.substring(0,4).concat(mg));
JSONObject jsonObject11 = jsonObject3.getJSONObject("CA");
name = jsonObject11.getString(quantity);
setCalcium(name.substring(0,4).concat(mg));
JSONObject jsonObject12 = jsonObject3.getJSONObject("MG");
name = jsonObject12.getString(quantity);
setMangnesium(name.substring(0,4).concat(mg));
JSONObject jsonObject13 = jsonObject3.getJSONObject("FE");
name = jsonObject13.getString(quantity);
setIron(name.substring(0,4).concat(mg));
JSONObject jsonObject14 = jsonObject3.getJSONObject("ZN");
name = jsonObject14.getString(quantity);
setZinc(name.substring(0,4).concat(mg));
JSONObject jsonObject15 = jsonObject3.getJSONObject("VITA_RAE");
name = jsonObject15.getString(quantity);
setVitaminA(name.substring(0,4).concat(ug));
JSONObject jsonObject16 = jsonObject3.getJSONObject("VITC");
name = jsonObject16.getString(quantity);
setVitaminC(name.substring(0,4).concat(mg));
JSONObject jsonObject17 = jsonObject3.getJSONObject("VITB6A");
name = jsonObject17.getString(quantity);
setVitaminB6(name.substring(0,4).concat(mg));
JSONObject jsonObject18 = jsonObject3.getJSONObject("VITD");
name = jsonObject18.getString(quantity);
setVitaminD(name.substring(0,4).concat(ug));
JSONObject jsonObject19 = jsonObject3.getJSONObject("VITB12");
name = jsonObject19.getString(quantity);
setVitaminB12(name.substring(0,4).concat(ug));
JSONObject jsonObject20 = jsonObject3.getJSONObject("VITK1");
name = jsonObject20.getString(quantity);
setVitaminK(name.substring(0,4).concat(ug));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Error"+error.toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
Picasso.get().load(image_url).fit().into(imageView);
textView.setText(name);
tabLayout = findViewById(R.id.tab_id);
tabLayout.addTab(tabLayout.newTab().setText("Ingredients"));
tabLayout.addTab(tabLayout.newTab().setText("Nutrition"));
tabLayout.addTab(tabLayout.newTab().setText("Or Refer"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = findViewById(R.id.pager_id);
final pagerAdapter pagerAdapter = new pagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(3);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这是寻呼机适配器
public class pagerAdapter extends FragmentPagerAdapter {
int noofTabs;
public pagerAdapter(android.support.v4.app.FragmentManager fm,int numberoftabs){
super(fm);
this.noofTabs=numberoftabs;
}
@Override
public Fragment getItem(int i) {
switch(i)
{
case 0 :
InfredientsTab infredientsTab = new InfredientsTab();
return infredientsTab;
case 1 :
FragmentNutrition fragmentNutrition = new FragmentNutrition();
return fragmentNutrition;
case 2 :
WebsiteRefered websiteRefered = new WebsiteRefered();
return websiteRefered;
default:
return null;
}
}
@Override
public int getCount() {
return noofTabs;
}
}