我有NavigationActivitiy有多个片段。
在第一个片段(MainFragment)中,有两个内部片段(ProductDisplayFragment,PaymentFragment)相互通信。在ProductDisplayFragment中选择项目时,所选产品将添加到ArrayList中,其中项目的ArrayList显示在PaymentFragment的ListView中。
但是,一旦我切换到另一个NavigationActivity片段,然后切换回MainFragment,ProductDisplayFragment仍然能够成功地将项目添加到PaymentFragment中的ArrayList(从logcat检查),但ListView不再显示其视图。
这是我的代码:
ProductDisplayFragment.java
public class ProductDisplayFragment extends Fragment {
private static final String MESSAGE_ERROR_IMPLEMENTATION = "Implementation of sendProduct method required";
private GridView gridView;
private ProductGridAdapter productAdapter;
private OnProductSelectedListener sendProduct;
public ProductDisplayFragment() {
// Required empty public constructor
}
public interface OnProductSelectedListener {
void onProductSelected(Product product);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_product_display, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridview);
productAdapter = new ProductGridAdapter(getActivity(), R.layout.fragment_product_display, getProductList());
gridView.setAdapter(productAdapter);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
sendProduct = (OnProductSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(MESSAGE_ERROR_IMPLEMENTATION);
}
}
}
PaymentFragment.java
public class PaymentFragment extends Fragment {
public PaymentFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null) {
productList = new ArrayList<>();
} else {
productList = savedInstanceState.getParcelableArrayList(KEY_PRODUCTLIST);
updateListView();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(KEY_PRODUCTLIST, productList);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_payment, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, productList);
listView.setAdapter(paymentAdapter);
voidButton = (LinearLayout) rootView.findViewById(R.id.button_void);
saveButton = (LinearLayout) rootView.findViewById(R.id.button_save);
noteButton = (LinearLayout) rootView.findViewById(R.id.button_note);
discountButton = (LinearLayout) rootView.findViewById(R.id.button_discount);
payButton = (LinearLayout) rootView.findViewById(R.id.button_pay);
amountSubtotal = (TextView) rootView.findViewById(R.id.amount_subtotal);
amountDiscount = (TextView) rootView.findViewById(R.id.amount_discount);
amountTotal = (TextView) rootView.findViewById(R.id.amount_total);
setDefaultAmount();
getFirstProduct();
buttonsOnClick();
return rootView;
}
/*
* This method retrieves product from fragment and refresh the listview every time a new product is added into listview
*/
public void getProduct(Product product) {
Log.d(CLASS_NAME, "Received subsequent product" + product.getName());
updateProductInfo(product);
updateListView();
double discount = Double.parseDouble(finalDiscount);
calculator.setDiscount(discount);
calculator.addAmount(product, currentMode);
amountSubtotal.setText(DOLLAR + String.format(PLACEHOLDER, calculator.getSubTotal()));
amountDiscount.setText(DOLLAR + String.format(PLACEHOLDER, calculator.getDiscountAmount()));
amountTotal.setText(DOLLAR + String.format(PLACEHOLDER, calculator.getTotal()));
}
private void updateProductInfo(Product product) {
// Only add product if it does not exist in hashset, else increment quantity number
if(productHash.contains(product.getBarcode())) {
int productIndex = productList.indexOf(product);
product.setQuantity(product.getQuantity() + 1);
if(productIndex != -1) {
productList.set(productIndex, product);
}
} else {
product.setQuantity(1);
productList.add(0, product);
productHash.add(product.getBarcode());
}
}
private void getFirstProduct() {
Bundle arguments = getArguments();
if (arguments != null) {
Product product = (Product) arguments.getSerializable(KEY_PRODUCT);
getProduct(product);
if(product != null) {
Log.d(CLASS_NAME, "Received subsequent product" + product.getName());
}
}
}
private void updateTextView () {
amountSubtotal.setText(DOLLAR + String.format(PLACEHOLDER, calculator.getSubTotal()));
amountDiscount.setText(DOLLAR + String.format(PLACEHOLDER, calculator.getDiscountAmount()));
amountTotal.setText(DOLLAR + String.format(PLACEHOLDER, calculator.getTotal()));
updateListView();
}
private void updateListView() {
Log.d(CLASS_NAME, "Update listview: " + productList.size());
paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, productList);
paymentAdapter.notifyDataSetChanged();
listView.setAdapter(paymentAdapter);
}
}