Android Java – Calling a Method From A Different Class to Delete Data from a Database Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android Java – Calling a Method From A Different Class to Delete Data from a Database without wasting too much if your time.

The question is published on by Tutorial Guruji team.

The following method is from the class DbProvider:

public void deleteVenue(UUID uuid)
{
    mDatabase.delete(VenueTable.NAME, VenueTable.Cols.UUID + " = ?",
    new String[] { uuid.toString() });
}

I need to call the above method from the class VenueDetailsFragment and more specifically from the onClick method of an Alert Dialog. This code is below:

package com.example.musicpro;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;

import java.util.UUID;

public class VenueDetailsFragment extends Fragment {

    private Venue mVenue;
    private Button mDoneButton;
    private Button mDeleteButton;
    private EditText mVenueName;
    private EditText mVenueAddress;
    private EditText mOpeningTime;

    private static final String ARG_VENUE_ID = "venue_id";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        UUID venueID = (UUID) getActivity().getIntent().getSerializableExtra(ARG_VENUE_ID);
        mVenue = DbProvider.get(getActivity()).getVenue(venueID);
    }

    @Override
    public void onPause()
    {
        super.onPause();

        mVenue.setVenueName(mVenueName.getText().toString());
        mVenue.setVenueAddress(mVenueAddress.getText().toString());
        mVenue.setOpeningTime(mOpeningTime.getText().toString());

        DbProvider.get(getActivity()).updateVenue(mVenue);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_venue_details, container, false);

        mVenueName = (EditText) v.findViewById(R.id.venue_name_edit);
        mVenueName.setText(mVenue.getVenueName());

        mVenueAddress = (EditText) v.findViewById(R.id.venue_address_edit);
        mVenueAddress.setText(mVenue.getVenueAddress());

        mOpeningTime = (EditText) v.findViewById(R.id.opening_time_edit);
        mOpeningTime.setText(mVenue.getOpeningTime());

        mDoneButton = (Button) v.findViewById(R.id.done_button);
        mDoneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v)
            {

            }
        });

        mDeleteButton = (Button) v.findViewById(R.id.delete_button);
        mDeleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog d = new AlertDialog.Builder(getActivity())
                        .setTitle(R.string.confirm_dialog)
                        .setMessage(R.string.delete_dialog)
                        .setPositiveButton(R.string.yes_dialog, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                //I need to call the deleteVenue method here
                            }
                        })
                        .setNegativeButton(R.string.no_dialog, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create();
                d.show();
            }
        });

        return v;
    }
}

Essentially, when a delete button is clicked, a dialog appears where the user confirms they want to delete. Once confirmed, the deleteVenue method should be called which removes the venue from the database and returns the user to a list view (instead of the detailed view) of the venues (with the deleted venue no longer listed).

If more code or information will assist please let me know. Thanks in advance.

Answer

You could create a field for your DbProvider, and set it within your onCreate instead of getting the instance whenever you need it.

private DbProvider mProvider;
private Venue mVenue;
// ...

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    UUID venueID = (UUID) getActivity().getIntent().getSerializableExtra(ARG_VENUE_ID);
    mProvider = DbProvider.get(getActivity());
    mVenue = mProvider.getVenue(venueID);
}

And then use it within your listener.

@Override
public void onClick(View v) {
    AlertDialog d = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.confirm_dialog)
            .setMessage(R.string.delete_dialog)
            .setPositiveButton(R.string.yes_dialog, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    // Delete the venue with its UUID
                    mProvider.deleteVenue(mVenue.getUUID());
                }
            })
            .setNegativeButton(R.string.no_dialog, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .create();
    d.show();
We are here to answer your question about Android Java – Calling a Method From A Different Class to Delete Data from a Database - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji