• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help How do i delete items from a sqlite databse?

rowlanda38

Lurker
I am creating a basic app where i can add customers to a database and display them on another screen, i have set up everything using recyclerview and card views to display the customers and that works fine, but im not sure how i set it up so that i can delete the customers. i have added a button to each of the customers that will be used to delete them.

customerDB
Java:
public class CustomerDB extends SQLiteOpenHelper
{
    // defines the database structure
    public static final String DATABASE_NAME = "customerDB.db";
    public static final String TABLE_NAME = "tbl_Customers";

    // creates the database
    public CustomerDB(Context context){ super(context, DATABASE_NAME, null, 1);}

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        sqLiteDatabase.execSQL("CREATE TABLE tbl_Customers  " +
                "(Customer_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                "Customer_First_Name TEXT,"  +
                "Customer_Surname TEXT, " +
                "Customer_Address_Line_1 TEXT,"  +
                "Customer_Address_Line_2 TEXT, " +
                "Customer_Address_Line_3 TEXT,"  +
                "Customer_Postcode TEXT, " +
                "Customer_Phone_Number TEXT,"  +
                "Customer_Email TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
    {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sqLiteDatabase);

    }


    public long addCustomer (String name, String surname, String address1, String address2, String address3, String postcode, String phoneNo, String email)
    {
        SQLiteDatabase Cdb = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("Customer_First_Name", name);
        contentValues.put("Customer_Surname", surname);
        contentValues.put("Customer_Address_Line_1", address1);
        contentValues.put("Customer_Address_Line_2", address2);
        contentValues.put("Customer_Address_Line_3", address3);
        contentValues.put("Customer_Postcode", postcode);
        contentValues.put("Customer_Phone_Number", phoneNo);
        contentValues.put("Customer_Email", email);

        long result = Cdb.insert("tbl_Customers", null, contentValues);
        Cdb.close();
        return result;
    }

    public Cursor ViewData()
    {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cust = sqLiteDatabase.rawQuery("select * from " + TABLE_NAME, null);

        return cust;
    }
   
}

customerAdapter

Java:
public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.MyHolder> {

    private Context context;
    private ArrayList id, name, surname, add1, add2, add3, postCode, phoneNumber, email;

    CustomerDB db = new CustomerDB(context);

    CustomerAdapter(Context context,ArrayList id, ArrayList name, ArrayList surname, ArrayList add1, ArrayList add2, ArrayList add3, ArrayList postCode, ArrayList phoneNumber, ArrayList email){
        this.context = context;
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.add1 = add1;
        this.add2 = add2;
        this.add3 = add3;
        this.postCode = postCode;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_row, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
        holder.idText.setText(String.valueOf(id.get(position)));
        holder.nameText.setText(String.valueOf(name.get(position)));
        holder.surnameText.setText(String.valueOf(surname.get(position)));
        holder.add1Text.setText(String.valueOf(add1.get(position)));
        holder.add2Text.setText(String.valueOf(add2.get(position)));
        holder.add3Text.setText(String.valueOf(add3.get(position)));
        holder.postCodeText.setText(String.valueOf(postCode.get(position)));
        holder.phoneNumberText.setText(String.valueOf(phoneNumber.get(position)));
        holder.emailText.setText(String.valueOf(email.get(position)));

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }


    @Override
    public int getItemCount() {
        return name.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder{

        TextView idText, nameText, surnameText, add1Text, add2Text, add3Text, postCodeText, phoneNumberText, emailText, delete;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            idText = itemView.findViewById(R.id.idText);
            nameText = itemView.findViewById(R.id.nameText);
            surnameText = itemView.findViewById(R.id.surnameText);
            add1Text = itemView.findViewById(R.id.add1Text);
            add2Text = itemView.findViewById(R.id.add2Text);
            add3Text = itemView.findViewById(R.id.add3Text);
            postCodeText = itemView.findViewById(R.id.postCodeText);
            phoneNumberText = itemView.findViewById(R.id.phoneNoText);
            emailText = itemView.findViewById(R.id.emailText);
            delete = itemView.findViewById(R.id.btnDel);
        }

    }
}

Customer Class

Java:
public class Customers extends AppCompatActivity {

    RecyclerView mRecyclerView;
    CustomerDB myDB;

    ArrayList<Integer> id;
    ArrayList<String> name, surname, add1, add2, add3, postCode, phoneNumber, email;

    CustomerAdapter customerAdapter;


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

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


        myDB = new CustomerDB(Customers.this);
        id = new ArrayList<Integer>();
        name = new ArrayList<>();
        surname = new ArrayList<>();
        add1 = new ArrayList<>();
        add2 = new ArrayList<>();
        add3 = new ArrayList<>();
        postCode = new ArrayList<>();
        phoneNumber = new ArrayList<>();
        email = new ArrayList<>();

        storeDataInArrays();
        customerAdapter = new CustomerAdapter(this, id, name, surname, add1, add2, add3, postCode, phoneNumber, email);

        mRecyclerView.setAdapter(customerAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


    }

    void storeDataInArrays() {
        Cursor cursor = myDB.ViewData();
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "No Customers", Toast.LENGTH_SHORT).show();
        } else {
            while ((cursor.moveToNext())) {
                id.add(cursor.getInt(0));
                name.add(cursor.getString(1));
                surname.add(cursor.getString(2));
                add1.add(cursor.getString(3));
                add2.add(cursor.getString(4));
                add3.add(cursor.getString(5));
                postCode.add(cursor.getString(6));
                phoneNumber.add(cursor.getString(7));
                email.add(cursor.getString(8));
            }
        }
    }

}

any help would be appreciated
 
Back
Top Bottom