MainActivity.java:

package com.example.appone;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    EditText etProduct;
    Button btnAdd;
    ListView listView;
    GridView gridView;

    DatabaseHelper databaseHelper;

    ArrayList<String> productList;
    ArrayAdapter<String> listAdapter;
    ArrayAdapter<String> gridAdapter;

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

        etProduct = findViewById(R.id.etProduct);
        btnAdd = findViewById(R.id.btnAdd);
        listView = findViewById(R.id.listView);
        gridView = findViewById(R.id.gridView);

        databaseHelper = new DatabaseHelper(this);

        productList = new ArrayList<>();

        listAdapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                productList
        );

        gridAdapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                productList
        );

        listView.setAdapter(listAdapter);
        gridView.setAdapter(gridAdapter);

        loadProducts();

        btnAdd.setOnClickListener(v -> {

            String productName = etProduct.getText().toString().trim();

            if (productName.isEmpty()) {
                etProduct.setError("Enter product name");
                etProduct.requestFocus();
                return;
            }

            boolean inserted = databaseHelper.insertProduct(productName);

            if (inserted) {

                Toast.makeText(
                        this,
                        "Product added successfully",
                        Toast.LENGTH_SHORT
                ).show();

                etProduct.setText("");

                loadProducts();

            } else {



                Toast.makeText(
                        this,
                        "Product insertion failed",
                        Toast.LENGTH_SHORT
                ).show();
            }
        });
    }

    private void loadProducts() {

        productList.clear();

        Cursor cursor = databaseHelper.getAllProducts();

        while (cursor.moveToNext()) {

            String productName = cursor.getString(1);

            productList.add(productName);
        }

        cursor.close();

        listAdapter.notifyDataSetChanged();
        gridAdapter.notifyDataSetChanged();
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="PRODUCT DATABASE"
        android:textSize="26sp"
        android:textStyle="bold"
        android:textColor="?android:attr/textColorPrimary"

        android:gravity="center"
        android:layout_marginBottom="20dp" />

    <EditText
        android:id="@+id/etProduct"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Product Name"
        android:inputType="text" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ADD PRODUCT"
        android:layout_marginTop="15dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Product List"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="5dp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="150dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Product Grid"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="5dp" />

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:horizontalSpacing="10dp"


        android:verticalSpacing="10dp" />

</LinearLayout>


DatabaseHelper.java:

package com.example.appone;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, "ProductDB", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE products (id INTEGER PRIMARY KEY AUTOINCREMENT, product_name TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS products");
        onCreate(db);
    }

    public boolean insertProduct(String productName) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("product_name", productName);
        long result = db.insert("products", null, values);
        return result != -1;
    }
    public Cursor getAllProducts() {
        SQLiteDatabase db = this.getReadableDatabase();
        return db.rawQuery("SELECT * FROM products", null);
    }
}
