在我的应用中,显示的购物车数量不会从mainactivity转移到detailsactivity。例如,如果我添加3个项目,并且在mainactivity页面的购物车上的textview中显示3,则当我转到详细信息页面时,textview变为0。已经有几天了,需要帮助。谢谢。
在第二次活动中调用onCreateOptionsMenu将textview再次设置为0时出现问题
public class MenuActivity extends AppCompatActivity {
public static TextView tv;
public static List<GeneralFood> cartFoods = new ArrayList<>();
public Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if (tv == null) {
MenuItem item = menu.findItem(R.id.action_addcart);
MenuItemCompat.setActionView(item, R.layout.cart_count_layout);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
tv = notifCount.findViewById(R.id.hotlist_hot);
View view = notifCount.findViewById(R.id.hotlist_bell);
cartUpdate();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MenuActivity.this, CartActivity.class);
startActivity(myIntent);
}
});
}else {
cartUpdate();
}
return true;
}
public static void cartUpdate() {
if (tv != null && cartFoods != null)
tv.setText(Integer.toString(cartFoods.size()));
}
@Override
protected void onResume() {
super.onResume();
cartUpdate();
}
}
详细信息:
public class Details extends MenuActivity {
TextView foodTitle, foodPrice, foodCalories, foodDescription, foodFat, foodSodium, foodProtein;
ImageView foodImage;
ImageButton foodDetailsPlus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Details");
toolbar.setTitleTextColor(0xFFFFFFFF);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Details.super.onBackPressed();
}
});
foodTitle = findViewById(R.id.food_title);
foodPrice = findViewById(R.id.food_price);
foodCalories = findViewById(R.id.food_calories);
foodDescription = findViewById(R.id.food_description);
foodFat = findViewById(R.id.food_fat);
foodSodium = findViewById(R.id.food_sodium);
foodProtein = findViewById(R.id.food_protein);
foodImage = findViewById(R.id.food_image);
foodDetailsPlus = findViewById(R.id.regular_food_plus_details);
foodTitle.setText(getIntent().getStringExtra("foodTitle"));
foodPrice.setText(Double.toString(getIntent().getDoubleExtra("foodPrice", 0)));
foodCalories.setText(Integer.toString(getIntent().getIntExtra("foodCalories", 0)));
foodDescription.setText(getIntent().getStringExtra("foodDescription"));
Picasso.get().load(getIntent().getStringExtra("foodImage")).fit().into(foodImage);
foodFat.setText(Integer.toString(getIntent().getIntExtra("foodFat", 0)));
foodSodium.setText(Integer.toString(getIntent().getIntExtra("foodSodium", 0)));
foodProtein.setText(Integer.toString(getIntent().getIntExtra("foodProtein", 0)));
}
}
MainActivity:
public class MainActivity extends MenuActivity {
RecyclerView recyclerViewHorizontal;
RecyclerView recyclerViewVertical;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Foodish");
toolbar.setTitleTextColor(0xFFFFFFFF);
recyclerViewHorizontal = findViewById(R.id.horizontal_recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerViewHorizontal.setLayoutManager(linearLayoutManager);
recyclerViewVertical = findViewById(R.id.vertical_recyclerview);
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerViewVertical.setLayoutManager(linearLayoutManager2);
RetrofitInterface retrofitService = RetrofitClient.getClient().create(RetrofitInterface.class);
Call<Food> call = retrofitService.getFoods();
call.enqueue(new Callback<Food>() {
@Override
public void onResponse(Call<Food> call, Response<Food> response) {
List<GeneralFood> popularFoods = response.body().getPopularFood();
recyclerViewHorizontal.setAdapter(new HorizontalAdapter(popularFoods, R.layout.recyclerview_horizontal, MainActivity.this));
List<GeneralFood> regularFoods = response.body().getRegularFood();
recyclerViewVertical.setNestedScrollingEnabled(false);
recyclerViewVertical.setAdapter(new VerticalAdapter(regularFoods, R.layout.recyclerview_vertical, getApplicationContext()));
}
@Override
public void onFailure(Call<Food> call, Throwable t) {
}
});
}
}