如何保持片段之间的分隔?

时间:2019-03-30 23:30:20

标签: android-fragments

我有一个抽屉菜单和3个片段(食物,杂货,电影),每个片段都是一个列表,还有一个添加新项目字段和添加按钮。

我希望能够在各开关之间进行切换,并使值保持分离。现在,当我将3项添加到“食物”列表中并且去杂货店时,我会看到相同的3项。

尝试很少的更改,但是没有运气

public class FoodFragment extends Fragment  {


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

private ArrayList<String> itemsFood;
private ArrayAdapter<String> itemsAdapterFood;
private ListView lvItemsFood;
private static final String TAG = "FoodFragment";


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

    Log.i(TAG, "In Food Fragment, onCreateView ");
    View view = inflater.inflate(R.layout.fragment_food, container, false);

    lvItemsFood = (ListView) view.findViewById(R.id.lvItemsFood);
    itemsFood = new ArrayList<String>();
    //readItems();
    itemsAdapterFood = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_list_item_1, itemsFood);
    lvItemsFood.setAdapter(itemsAdapterFood);
    if (itemsFood.isEmpty())
        itemsFood.add("Dummy Item");

    final EditText newTask    = (EditText) view.findViewById(R.id.etNewItemFood);
    Button btnAdd = (Button) view.findViewById(R.id.btnAddItemFood);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String itemText = newTask.getText().toString();
            itemsAdapterFood.add(itemText);
            newTask.setText("");
            //writeItems();
            Log.i(TAG, "In Food Fragment, send data "+itemText);
            Log.i(TAG, "In Food Fragment, at the end of setOnClickListener: " + itemsFood.toString());
        }
    });

    Log.i(TAG, "In Food Fragment, at the end of onCreateView: " + itemsFood.toString());
    ((MainActivity) getActivity()).setItems(itemsFood);

    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    Log.i(TAG, "In Food Fragment, onViewCreated ");

    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.i(TAG, "In Food Fragment, onActivityCreated ");

    super.onActivityCreated(savedInstanceState);

    OnItemLongClickListener listener = new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
            itemsFood.remove(position);
            itemsAdapterFood.notifyDataSetChanged();
            //writeItems();
            //return true;
            return false;
        }
    };
    lvItemsFood.setOnItemLongClickListener(listener);
}



private void readItems() {
    File filesDir = getContext().getFilesDir();
    File todoFile = new File(filesDir, "todo.txt");
    try {
        itemsFood = new ArrayList<String>(FileUtils.readLines(todoFile));
    } catch (IOException e) {
        itemsFood = new ArrayList<String>();
    }
}

private void writeItems() {
    File filesDir = getContext().getFilesDir();
    File todoFile = new File(filesDir, "todo.txt");
    try {
        FileUtils.writeLines(todoFile, itemsFood);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我希望能够保留3个单独的列表,并能够单击工具栏中的共享按钮来共享我所在的列表。

0 个答案:

没有答案