-1

Just trying to create a simple app that would allow users to input notes and save them into the List view. What code would I need to implement to allow the user to longClick something in the list to delete it.

public class Notes extends AppCompatActivity {

Button save;
ArrayList<String> addArray = new ArrayList<String>();
EditText txt;
ListView show;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes);

    txt = (EditText) findViewById(R.id.textInput);
    show = (ListView) findViewById(R.id.listView);
    save = (Button) findViewById(R.id.saveButton);

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Notes.this, android.R.layout.simple_list_item_1, addArray);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String getInput = txt.getText().toString();

            if (addArray.contains(getInput)) {
                Toast.makeText(getBaseContext(), "Note already added!", Toast.LENGTH_LONG).show();
            } else if (getInput == null || getInput.trim().equals("")) {
                Toast.makeText(getBaseContext(), "Input required!", Toast.LENGTH_LONG).show();
            } else {
                addArray.add(getInput);

                show.setAdapter(adapter);
                ((EditText) findViewById(R.id.textInput)).setText(" ");
            }
        }
    });


    show.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {

            return true;
        }
    });
}

}

Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
Mike
  • 67
  • 1
  • 2
  • 11

2 Answers2

0

That might solve your problem:

addArray.remove(pos);
adapter.notifyOnDataSetChanged();
g.kanellis
  • 140
  • 2
  • 10
0

You could make use of Contextual Menus or Contextual Action Mode.

Go through this tutorial : http://developer.android.com/guide/topics/ui/menus.html#context-menu

Then you could delete the item from adapter and notify lostview using adapters Notifydatasetchanged method.

Arun Shankar
  • 2,603
  • 2
  • 26
  • 36