'Questions regarding CRUD operations

I am new to Android Studio and I am training to do a CRUD. When I try to insert a plant in the database, the message "Plant inserted" does not appear. Do you have an idea of ​​the problem ? However, I display the message with the following code: Toast.makeText(AddActivity.this, "Plant inserted", Toast.LENGTH_SHORT).show();

Class PlantDataSource :

public class PlantDataSource extends SQLiteOpenHelper {

    private SQLiteDatabase db;
    private DatabasePlant dbPlant;

    public PlantDataSource(Context context) {
        super(context, "dbPlant", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String strReq = "CREATE TABLE tab_plant (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)";
        db.execSQL(strReq);
    }

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


    public boolean insertPlant (Plant plant) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", plant.getNom());
        long result = db.insert("tab_plant", null, values);
        boolean insert = result == -1 ? false : true;
        return insert;
    }

    public Cursor getAllPlants() {
        db = this.getWritableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM tab_plant", null);
        return res;
    }

}

Class ViewActivity :

public class ViewActivity extends AppCompatActivity {

    private PlantDataSource plantBdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        plantBdd = new PlantDataSource(this);
        viewAll();
    }

    public void viewAll() {
        Cursor res = plantBdd.getAllPlants();
        if (res.getCount() == 0) {
            showMessage("Error", "No plants to display");

        }
        StringBuffer buffer = new StringBuffer();
        while (res.moveToNext()) {
            buffer.append("ID " + res.getString(0) + "\n");
            buffer.append("NAME " + res.getString(1) + "\n");
        }
        showMessage("Data", buffer.toString());
    }

    public void showMessage(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
}

Class AddActivity :

public class AddActivity extends AppCompatActivity {
    private EditText mPlantname;
    private Button mAddBtn;
    private  PlantDataSource plantBdd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        mPlantname = (EditText) findViewById(R.id.activity_add_plantname);
        mAddBtn = (Button) findViewById(R.id.activity_add_addBtn);
        plantBdd = new PlantDataSource(this);
        addPlant();
    }

    public void addPlant() {
        mAddBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Plant plant = new Plant(mPlantname.getText().toString());
                    boolean isInserted = plantBdd.insertPlant(plant);
                    Log.i("***", String.valueOf(isInserted));
                    Log.i("xxx", String.valueOf(plant));

                    if (isInserted) {
                        Toast.makeText(AddActivity.this, "Plant inserted", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source