У меня есть меню ящика и 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 отдельных списка и иметь возможность щелкнуть кнопку «Поделиться» на панели инструментов и поделиться списком, в котором я нахожусь.