我正在创建一个在线食品订单应用程序,我在一个视图寻呼机中以单个片段(不是列表形式)显示单个项目但是当我尝试添加项目的数量时,则更改其他项目中的显示(即显示的更改)在其他片段)数量不在同一项目,因为有这么多项目。
如何管理每件商品的数量,以便更改在同一商品中显示而不是在其他商品中显示,而在此代码中产生问题的另一件事是我刷物品然后重置数量。我想知道如何在视图寻呼机片段中添加或删除单个项目的数量。
这是我的代码:
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int position) {
PlaceholderFragment fragment = new PlaceholderFragment();
String itemTitle = foodItemTitle.get(position);
String itemPrice = foodItemPrice.get(position);
String itemImage = foodItemImage.get(position);
Bundle args = new Bundle();
args.putString("ITEMS", itemTitle);
args.putString("PRICE", itemPrice);
args.putString("IMAGE", itemImage);
fragment.setArguments(args);
return fragment;
}
public static void setContext( Activity activity) {
context = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_items_list, container, false);
img = (ImageView) rootView.findViewById(R.id.section_label);
pricePerItem = rootView.findViewById(R.id.price);
nutri = rootView.findViewById(R.id.itemName);
removeBtn=rootView.findViewById(R.id.removebtn);
addBtn=rootView.findViewById(R.id.addBtn);
quantity=rootView.findViewById(R.id.qty);
String items = getArguments().get("ITEMS").toString();
final String price = getArguments().get("PRICE").toString();
String image = getArguments().get("IMAGE").toString();
setItemDetails(items, price, image);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
countTtl = count + 1;
int curntQty = Integer.parseInt(quantity.getText().toString()) + 1;
Toast.makeText(getContext(), String.valueOf(curntQty), Toast.LENGTH_SHORT).show();
quantity.setText(String.valueOf(curntQty));}});
removeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quantity.getText().toString().equals("0")) {
Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
} else {
int qtyTtl = Integer.parseInt(quantity.getText().toString()) - 1;
quantity.setText(String.valueOf(qtyTtl));
Toast.makeText(getContext(), quantity.getText().toString(), Toast.LENGTH_SHORT).show(); }
}
});
return rootView;
}
}
private static void setItemDetails(String item, String price, String image) {
String[] itemStr = item.split(",");
String[] itemPriceStr = price.split(",");
String[] imageStr = image.split(",");
for (int i = 0; i < itemStr.length; i++) {
String oneItem = itemStr[i].trim();
oneItem = oneItem.replace("[", "");
oneItem = oneItem.replace("]", "");
nutri.setText(oneItem);
String Price = itemPriceStr[i].trim();
String perImage = imageStr[i].trim();
Price = Price.replace("[", "");
Price = Price.replace("]", "");
pricePerItem.setText(Price);
perImage = perImage.replace("[", "");
perImage = perImage.replace("]", "");
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 0);
}
@Override
public int getCount() {
// Show 3 total pages.
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);}}
如果我添加片段的项目
,我想显示数量答案 0 :(得分:0)
如果我理解您的代码正确Joining, by = "id"
Error: object 'y.id' not found
并且您使用quantity=rootView.findViewById(R.id.qty);
设置新数量,则只有当您的rootView包含多个片段时,数量才能指向不同的片段(并因此查看)。