I'm trying to build a to-do list in android studio but I don't quite get how to go about deleting one of the tasks already on the app.
As you can see in the code below, I'm using an arraylist to store tasks and add new ones and a list view to display the arraylist. Does anyone know how I could delete a task here? thank you in advance!
package com.example.todolist;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainJava extends AppCompatActivity implements AdapterView.OnItemClickListener {
// 1. Some variables and definitions
ListView listView;
ArrayList<MyData> arrayList = new ArrayList<>();
MyAdapter adapter;
private EditText taskEntered;
/* Define Menu item identifiers.
Menu.FIRST is a constant of the Menu class used to define first menu item,
used by convention
*/
final int ADD = Menu.FIRST;
final int DELETE = Menu.FIRST+ 1;
final int UPDATE = Menu.FIRST+ 2;
final int SAVE = Menu.FIRST+ 3;
final int CLOSE = Menu.FIRST+ 4;
// 2. Method: The classic ON CREATE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// locate the widgets: edit text (to get string) and list view (to put string)
taskEntered = (EditText) findViewById(R.id.enterTask);
listView = findViewById(R.id.listView);
// attach listener to list view
listView.setOnItemClickListener(this);
taskEntered.setText("");
}
// 3. Method: ON CREATE OPTIONS MENU
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item1 = menu.add(0, ADD, Menu.NONE, "ADD");
MenuItem item2 = menu.add(0, DELETE, Menu.NONE, "DELETE");
MenuItem item3 = menu.add(0, UPDATE, Menu.NONE, "UPDATE");
MenuItem item4 = menu.add(0, SAVE, Menu.NONE, "SAVE");
MenuItem item5 = menu.add(0, CLOSE, Menu.NONE, "CLOSE");
return true;
}
// 4. Method: ON OPTIONS SELECTED
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId(); // get id of menu item picked
switch (itemID) {
case ADD :
addNewTask(); // prints entered task in list view
return true;
case DELETE :
// not sure what to add here
deleteTask();
return true;
case CLOSE : finish(); // close will be updated later
default: super.onOptionsItemSelected(item);
}
return false;
}
// 5. Method: PRINT NEW TASK in list view and ADD NEW TASK to arrayList
public void addNewTask() {
// get string from the edit text
String taskString = taskEntered.getText().toString();
// create ArrayList values
arrayList.add(new MyData(arrayList.size(), taskString));
// TESTING: arrayList.add(new MyData(2, " Robert"));
//create custom adapter and connect to ListView
adapter = new MyAdapter(this, arrayList);
listView.setAdapter(adapter);
taskEntered.setText(""); // clear edit text bar after adding item
}
// 6. Method: Listener, when you click on an item in list view it does this:
// listener is passed into the position of the item that was clicked?
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// you click on an item in list view
String task = arrayList.get(position).getName(); // get name of task
int numTask = arrayList.get(position).getNum(); // get number of task
// set edit text field to show the item you just clicked
taskEntered.setText(numTask + " " + task);
}
// 7. Method: Delete a task
public void deleteTask() {
//help here
// get string from the edit text
String taskString = taskEntered.getText().toString();
// create ArrayList values - how do i make it be a loop ??
arrayList.remove();
// TESTING: arrayList.add(new MyData(2, " Robert"));
//create custom adapter and connect to ListView
adapter = new MyAdapter(this, arrayList);
listView.setAdapter(adapter);
}
}
Ended up using this method that worked!
For this to work I had to update the onItemClick method to keep track of the position of the item selected (Within the arraylist). Once I had the position of the item I could just use arrayList.remove(itemPos) to delete it.