0

I actually did part of this code on a normal activity but when I want to make it into a fragment class, I encountered some problem I did what I can to convert the code into a fragment class.

this is the fragment code

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingLstFragment extends Fragment {


    public ShoppingLstFragment() {
        // Required empty public constructor
    }
    private ArrayList<String> arraylisttodo;
    private ArrayAdapter<String> arrayadaptertodo;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shopping_lst, container, false);
        arraylisttodo = new ArrayList<String>();
        arrayadaptertodo = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_activated_1 , arraylisttodo);
        ListView listViewtodo = (ListView) view.findViewById(R.id.listviewtodo);
        listViewtodo.setAdapter(arrayadaptertodo);

        registerForContextMenu(listViewtodo);

        try {
            Log.i("ON CREATE", "on create has occured");
            Scanner scanner = new Scanner(getContext().openFileInput("todo.txt"));

            while (scanner.hasNextLine()){
                String todo = scanner.nextLine();
                arrayadaptertodo.add(todo);
            }
            scanner.close();
        }catch (Exception e){
            Log.i("ON CREATE", e.getMessage());
        }

        return view;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        if(v.getId() != R.id.listviewtodo) {
            return;
        }
        menu.setHeaderTitle("What would you like to do?");
        String[] options = {"Delete Product", "Return"};

        for (String option : options) {
            menu.add(option);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int selectedIndex = info.position;

        if (item.getTitle().equals("Delete Product")) {
            arraylisttodo.remove(selectedIndex);
            arrayadaptertodo.notifyDataSetChanged();
        }
        return true;
    }


    public void onBackPressed() {
        try {
            Log.i("ON BACK PRESSED", "On back is pressed");
            PrintWriter pw = new PrintWriter(getContext().openFileOutput("todo.txt", Context.MODE_PRIVATE));

            for(String todo : arraylisttodo){
                pw.println(todo);
            }
            pw.close();
        } catch(Exception e) {
            Log.i("ON BACK PRESSED", e.getMessage());
        } getActivity().onBackPressed();
    }


    public void buttonaddClick(View v){
            EditText todo = (EditText)getView().findViewById(R.id.todo);
        String toDo = todo.getText().toString().trim();


        if (toDo.isEmpty()) {

            arrayadaptertodo.add(toDo);
            todo.setText("");
        }


        }}

these methods onBackPressed() and buttonaddclick() is being said not used.

I'm new to android and java. how do you solve this error?

Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50
Jason
  • 51
  • 8

1 Answers1

0

For onBackPressed() method you will need to take a look at this Question

And for buttonaddclick() method, I think you will need to code android:onClick"buttonaddclick" in your fragment_shopping_lst layout if not then show me you XML code, Also if you need any other help comment to me

Community
  • 1
  • 1
Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50
  • @Jason Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the [About] page and also [How do I ask questions here?](http://stackoverflow.com/help/how-to-ask) – Dasser Basyouni Jan 09 '17 at 11:25