mardi 19 mai 2015

Radiobutton and Checkbox to SQL database in android

I'm working in Android Studio and I'm setting up a SQLite database in order to store the users data in order to show it again later. I figured out as to how to do it with simple TextEdits, but how do i do it for the Radio Button and CheckBoxes ? In my program there is two Radio Buttons, one for yes and one for no. The Checkboxes' are just attached to a textstring which i want to showcase. Thanks for your help !

My DatabaseHelper looks like:

public class DatabaseHelper  extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "Events.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "TITLE";
public static final String COL_3 = "DATE";
public static final String COL_4 = "REMINDER";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1); //superclass (context, name, factory, version)
}

@Override
public void onCreate(SQLiteDatabase db) {
    //
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, DATE TEXT, REMINDER 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 title, String date, String reminder){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, title);
    contentValues.put(COL_3, date);
    contentValues.put(COL_4, reminder);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1)
            return false;
        else
            return true;
    }
public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME, null);
    return res;
     }
}

and my MainActivity looks like:

public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
EditText edittitle, editdate, editreminder;
Button btnAddData;
Button btnViewAll;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DatabaseHelper(this);

    edittitle = (EditText) findViewById(R.id.et_title);
    editdate = (EditText) findViewById(R.id.et_date);
    editreminder = (EditText) findViewById(R.id.et_reminder);
    btnAddData = (Button) findViewById(R.id.b_add);
    btnViewAll = (Button) findViewById(R.id.b_view);
    AddData();
    ViewAll();
}

public void AddData() {
    btnAddData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDb.insertData(edittitle.getText().toString(),
                            editdate.getText().toString(),
                            editreminder.getText().toString());
                    if (isInserted = true)
                        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this, "Data NOT Inserted", Toast.LENGTH_LONG).show();
                }
            }
    );
}

public void ViewAll() {
    btnViewAll.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    if (res.getCount() == 0) {
                        //show message
                        showMessage("Error", "No data found");
                        return;
                    }

                    StringBuffer buffer = new StringBuffer();
                    while (res.moveToNext()) {
                        buffer.append("id :" + res.getString(0) + "\n");
                        buffer.append("title :" + res.getString(1) + "\n");
                        buffer.append("date :" + res.getString(2) + "\n");
                        buffer.append("reminder :" + res.getString(3) + "\n");
                    }

                    //show all data
                    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();
}

}




Aucun commentaire:

Enregistrer un commentaire