当片段切换到任何其他片段或应用程序关闭并再次运行时,ArrayList 会被销毁

问题描述

在我的 android 应用程序中,我有一个包含 5 个片段的导航视图。在一个片段中,我有一个显示在列表视图中的数组列表,当我打开片段时它工作正常,但是当我将此片段更改为任何其他片段或关闭应用程序然后再次运行该应用程序时,数组列表变空,不显示任何内容。

我不确定是否与 savedInstanceState 和 SharedPreferences 有关系,但不知道如何解决这个问题。

这是我对该片段的代码:

public class ShoppingListFragment extends Fragment {

    private ListView listView;
    EditText editText;
    ImageButton imageButton;
    CheckBox checkBox;
    private View view;

    ArrayList<String> list;
    ArrayAdapter adapter;

    public ShoppingListFragment() {
        // Required empty public constructor
    }

    public static ShoppingListFragment newInstance(String param1,String param2) {
        ShoppingListFragment fragment = new ShoppingListFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null) {
            //Restore the fragment's state here
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

        if (view != null) {
            return view;
        }
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_shopping_list,container,false);

        editText = (EditText) view.findViewById(R.id.EtxtItem);
        imageButton = (ImageButton) view.findViewById(R.id.Imgbtnadd);
        checkBox = (CheckBox) view.findViewById(R.id.checkbox);

        listView = view.findViewById(R.id.ShoppingListView);


        list = new ArrayList<>();
        adapter = new ArrayAdapter<String>(getActivity(),R.layout.shopping_list_single_row,R.id.AddShoppingItem,list);
        listView.setAdapter(adapter);

        setHasOptionsMenu(true);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String item = editText.getText().toString();
                if (!item.isEmpty()) {
                    list.add(item);
                    adapter.notifyDataSetChanged();
                    editText.setText("");
                    Toast.makeText(getContext(),item + " added to Shopping List",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getContext(),"Please add an Item",Toast.LENGTH_LONG).show();
                }
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView,View view,int position,long l) {

                final EditText UpdateList = new EditText(getActivity());
                UpdateList.setInputType(InputType.TYPE_CLASS_TEXT);
                UpdateList.setText(list.get(position));

                new AlertDialog.Builder(getActivity())
                        .setIcon(R.drawable.ic_list_foreground)
                        .setTitle(list.get(position))
                        .setPositiveButton("Update",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface,int i) {
                                new AlertDialog.Builder(getActivity())
                                        .setTitle("Update")
                                        .setView(UpdateList)
                                        .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface,int i) {
                                                list.set(position,UpdateList.getText().toString());
                                                adapter.notifyDataSetChanged();
                                                Toast.makeText(getActivity(),"Updated",Toast.LENGTH_LONG).show();
                                            }
                                        })
                                        .setNegativeButton("Cancel",null)
                                        .show();
                            }
                        })
                        .setNegativeButton("Delete",int i) {
                                /* UPDATE THIS */
                                String val = adapterView.getItemAtPosition(position).toString();
                                list.remove(position);
                                adapter.notifyDataSetChanged();
                                Toast.makeText(getActivity(),"Deleted",Toast.LENGTH_LONG).show();
                            }
                        })
                        .setNeutralButton("Cancel",null)
                        .show();

                return true;
            }
        });
        return view;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu,@NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.delete,menu);
        MenuItem item = menu.findItem(R.id.SlistDeleteAll);
        item.getActionView();
        super.onCreateOptionsMenu(menu,inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.SlistDeleteAll) {
            if (list.size() > 0) {
                list.clear();
                adapter.notifyDataSetChanged();
                Toast.makeText(getActivity(),"Shopping List Cleared",Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getActivity(),"Shopping List Empty",Toast.LENGTH_LONG).show();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    int mCurCheckPosition = 0;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Save the fragment's state here
    }
}

我是 Android 应用开发的新手。因此,如果有人可以对此提供帮助,那就太好了。 提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)