How to Delete a list view row with SQLite data by click

Solution for How to Delete a list view row with SQLite data by click
is Given Below:

My code won’t delete a row from the database. Which block do I have to change?

ListActivity.Java

This is the Section where I code the delete code. I get stuck here.

public class ListDataActivity extends AppCompatActivity {
    private ListView listView;
    private DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(this);
        listView = findViewById(R.id.listViewId);

        loadData();

    }

    public  void loadData() {

        ArrayList<String> listData = new ArrayList<>();

        Cursor cursor =  databaseHelper.displayAllData();

        if(cursor.getCount() == 0){
            Toast.makeText(getApplicationContext(),"no data is available",Toast.LENGTH_LONG).show();
        }else{
            while (cursor.moveToNext()){
                listData.add(cursor.getString(0)+" t "+cursor.getString(1)+" t"+cursor.getString(2));
            }
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.list_item,R.id.textViewId,listData);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedValue = adapterView.getItemAtPosition(i).toString();
                Toast.makeText(getApplicationContext(),"Selected Value : "+selectedValue,Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getApplicationContext(),ListDataActivity.class);

                AlertDialog.Builder alert = new AlertDialog.Builder(ListDataActivity.this);
                alert.setTitle("Delete");
                alert.setMessage("Do you want to delete this item from list?");
                alert.setCancelable(false);

                alert.setNegativeButton("No", new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listData.remove(i);
                        adapter.notifyDataSetChanged();

                    }
                });

                AlertDialog alertDialog = alert.create();
                alertDialog.show();

            }
        });


    }

}

Database.Java

public class DatabaseHelper  extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "student.db";
    private static final String TABLE_NAME = "student_details";

    private static final String ID = "_id";
    private static final String NAME = "Name";
    private static final String PHONE = "Phone";
    private static final int VERSION_NUMBER = 5;


    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + NAME + " VARCHAR(255) NOT NULL," + PHONE + " INTEGER(11) NOT NULL )";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
    private static final String SELECT_ALL = "SELECT * FROM " + TABLE_NAME;


    private Context context;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION_NUMBER);
        this.context = context;
    }

    public int deleteData(String name) {

            SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
            int value = sqLiteDatabase.delete(TABLE_NAME, NAME + " = ", new String[]{name});
            return value;
        }

}

How to delete a row from listview as I have used click listener is there any way to delete the row from the listview. I used SQLite Database in my project.

This is My Answer:

public int deleteData(String name) {

            SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
            int value = sqLiteDatabase.delete(TABLE_NAME, ID + " = ?", new String[]{name});
            return value;
        }