我是Android编程新手。我正在实现扫描特定库存项目的功能,并搜索条形码(如果它已存在于ArrayList中)。如果它存在,那么我用InvnetoryFound片段替换当前的Scan片段,其中我可以选择更新数量并从ArrayList中完全删除项目。
我面临的问题是,当我再次扫描不同的库存条形码时删除库存后,扫描成功后,它显示的InventoryFound片段包含从ArrayList中删除的旧数据。以下是我的代码:
测试ArrayList代码:
public static String[] inve = new String[5];
public static ArrayList<String[]> inventory_list = new ArrayList<String[]>();
inve[0] = "INV45879";
inve[1] = "Bridge Support Pipe";
inve[2] = "Pre-insulated pipe supports provide both support and insulation in hot and cold piping applications.Pre-insulated pipe supports provide both support and insulation in hot and cold piping applications.";
inve[3] = "0885370720679";
inve[4] = "2";
inventory_list.add(inve);
inve = new String[5];
inve[0] = "INV45880";
inve[1] = "Cement";
inve[2] = "Pre-insulated pipe supports provide both support and insulation in hot and cold piping applications";
inve[3] = "9771234567003";
inve[4] = "8";
inventory_list.add(inve);
如果扫描成功,则调用以下函数来替换片段:
private static void showFragment(Fragment fragment, String back_option) {
try {
// start transition
FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
//if fragment Inventory is found then pass data to fragment
if(back_option.equals("inventoryFound")){
//sends found inventory list data to fragment
String[] inv_data = inventory_list.get(inventory_index);
Bundle bundle = new Bundle();
bundle.putStringArray("inventory_data",inv_data);
bundle.putInt("inventory_index",inventory_index);
inventoryFoundFragment.setArguments(bundle);
back_option = "null";
// replace fragment
if (!back_option.equals("null")) // with back option
ft.replace(R.id.frag_content, fragment).addToBackStack(back_option);
else // no back option
ft.replace(R.id.frag_content, fragment);
// commit
ft.commit();
}
catch(Exception e){
out("PROBLEM SHOWING FRAGMENT!!!"+e.getMessage());
}
}
InventoryFound片段填充数据并正常工作。下面是我点击删除已填充库存时的代码:
public void removeInventory(){
Shared.inventory_list.remove(inventory_index);
Toast.makeText(mContext,"Inventory "+inventory_index+" Removed Successfully!", Toast.LENGTH_SHORT).show();
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(this);
ft.replace(R.id.frag_content, new ScanFragment());
ft.commit();
}
它已成功删除,但是当我再次扫描时,例如条形码&#34; 0885370720679&#34;它显示了InventoryFound片段上的先前数据。我在Logcat(Log.d)中调试了它,我能够看到正确的数据,但它没有显示在片段中。
任何建议或指导将不胜感激。
答案 0 :(得分:0)
我找到了解决这个问题的方法。
当Fragment位于前台时,它会调用onResume()方法。在这个方法中,我调用用户定义的函数,它重新设置Fragment字段的数据。我不知道解决这个问题是否正确,但它现在正在为我工作!