Search code examples
androidsqlitedateandroid-sqliteandroid-spinner

Dialog(Date) & Spinner(Dropdown) can be consider as a string? How to Store it into database? I try to explain more what i'm facing


I've create a String for (dialog)date in database, and one String for Spinner(dropdown). and I'm trying to store this two in database. but it seems not working because of these are not String. But in Sqlite, There is no 'Date time'to store.

From Databasehelper

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Person.db";
    public static final String TABLE_NAME = "Person_Table";
    public static final String COL_1 = "OID";
    public static final String COL_2 = "Date";
    public static final String COL_3 = "Description";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (OID INTEGER PRIMARY KEY AUTOINCREMENT," +
               Description TEXT)");
    }

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

    public boolean insertData(String Date, String Description) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, Date);
        contentValues.put(COL_3, Description);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

}

After Clicking the Save button. It should store into database. But I kept clicking and it showed "Data Not Inserted". Is (dialog)Date and (spinner)Dropdown can't consider as String? So it wouldn't store into database?

From JavaPage

Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isInserted = myDb.insertData(
                        Display_date.getText().toString().trim(),
                        e_Description.getText().toString());

                if (isInserted == true)
                    Toast.makeText(Donation.this, "Data Inserted", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(Donation.this, "Data not Inserted", Toast.LENGTH_LONG).show();

            }
        });

Here is the (Dialog)Date CODE

 Display_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);

                DatePickerDialog dialog = new DatePickerDialog(Donation.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth, DataListener,
                        year, month, day);

                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });
        DataListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                month = month + 1;
                Log.d(TAG, "onDateSet: mm/dd/yyyy: " + month + "/" + day + "/" + year);
                String date = month + "/" + day + "/" + year;
                Display_date.setText(date);
            }
        };

Here is the (Spinner) Dropdown CODE

public void dropdown() {

        Spinner dropdown = findViewById(R.id.s_Description);
        String[] items = new String[]{"Beverage", "food"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);
    }

Solution

  • Your table has just 2 columns 1 for id and 2nd for description.You are trying to insert your description in id column and date in description column. Hence it will be giving exception.

    To correct it, create 1 more column in your database for datetime of String type and then try inserting your data again.

    Remember to uninstall your current app before trying again as tables once created can't be updated unless you use that code in onUpgrade() method and for onUpgrade() to get called you have to keep version codes.