Advertisement
Guest User

Untitled

a guest
May 5th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. package com.thenewboston;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. public class SqlLiteExample extends Activity implements OnClickListener{
  12. EditText etName,etHotness;
  13. Button btnSave,btnView;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.sqliteexample);
  19. etName=(EditText) findViewById(R.id.editText1);
  20. etName=(EditText) findViewById(R.id.editText2);
  21. btnSave=(Button) findViewById(R.id.btnSQLUPDATE);
  22. btnView=(Button) findViewById(R.id.btnSQLVIEW);
  23. btnSave.setOnClickListener(this);
  24. btnView.setOnClickListener(this);
  25. }
  26. @Override
  27. public void onClick(View v) {
  28. // TODO Auto-generated method stub
  29.  
  30. switch(v.getId()){
  31.  
  32. case R.id.btnSQLUPDATE:
  33. boolean didItWork=true;
  34. try{
  35. String name=etName.getText().toString();
  36. String hotness=etHotness.getText().toString();
  37. HotOrNot entry= new HotOrNot(SqlLiteExample.this);
  38. entry.open();
  39. entry.createEntry(name,hotness);
  40. entry.close();
  41. }catch(Exception e){
  42. didItWork= false;
  43. }finally{
  44. if(didItWork){
  45. Dialog d= new Dialog(this);
  46. d.setTitle("Heck yea");
  47. System.out.println("testing");
  48. TextView tv= new TextView(this);
  49. tv.setText("sucess");
  50. d.setContentView(tv);
  51. d.show();
  52. }
  53. }
  54.  
  55.  
  56. break;
  57. case R.id.btnSQLVIEW:
  58.  
  59. break;
  60. }
  61.  
  62. }
  63.  
  64.  
  65. }
  66.  
  67. package com.thenewboston;
  68.  
  69. import android.content.ContentValues;
  70. import android.content.Context;
  71. import android.database.SQLException;
  72. import android.database.sqlite.SQLiteDatabase;
  73. import android.database.sqlite.SQLiteOpenHelper;
  74.  
  75. public class HotOrNot {
  76. public static final String KEY_ROWID = "_id";
  77. public static final String KEY_NAME = "persons_name";
  78. public static final String KEY_HOTNESS = "persons_hotness";
  79.  
  80. private static final String DATABASE_NAME = "HotOrNotdb";
  81. private static final String DATABASE_TABLE = "peopleTable";
  82. private static final int DATABASE_VERSION = 1;
  83.  
  84. private DbHelper ourhelper;
  85. private final Context ourContext;
  86. private SQLiteDatabase ourDatabase;
  87.  
  88. private static class DbHelper extends SQLiteOpenHelper {
  89.  
  90. public DbHelper(Context context) {
  91. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  92. // TODO Auto-generated constructor stub
  93. }
  94.  
  95. @Override
  96. public void onCreate(SQLiteDatabase db) {
  97. // TODO Auto-generated method stub
  98. db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
  99. + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
  100. + " TEXT NOT NULL, " + KEY_HOTNESS + "TEXT NOT NULL);"
  101.  
  102. );
  103. }
  104.  
  105. @Override
  106. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  107. // TODO Auto-generated method stub
  108. db.execSQL("DROP TABLE IF EXIST "+ DATABASE_TABLE);
  109. onCreate(db);
  110.  
  111. }
  112.  
  113. }
  114.  
  115. public HotOrNot(Context c) {
  116. ourContext = c;
  117. }
  118. public HotOrNot open() throws SQLException{
  119. ourhelper = new DbHelper(ourContext);
  120. ourDatabase= ourhelper.getWritableDatabase();
  121. return this;
  122. }
  123. public void close(){
  124. ourhelper.close();
  125. }
  126. public long createEntry(String name, String hotness) {
  127. // TODO Auto-generated method stub
  128. ContentValues cv= new ContentValues();
  129. cv.put(KEY_NAME, name);
  130. cv.put(KEY_HOTNESS, hotness);
  131. return ourDatabase.insert(DATABASE_TABLE, null, cv);
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement