daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Feb 21st, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.view.View.OnClickListener;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8.  
  9. public class MainActivity extends Activity implements OnClickListener {
  10.  
  11. TextView listContent;
  12. EditText edt;
  13.  
  14. private SQLiteAdapter mySQLiteAdapter;
  15.  
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19.     super.onCreate(savedInstanceState);
  20.     setContentView(R.layout.activity_main);
  21.     listContent = (TextView) findViewById(R.id.textView1);
  22.     edt = (EditText) findViewById(R.id.title);
  23.     Button save = (Button) findViewById(R.id.confirm);
  24.     save.setOnClickListener(this);
  25.  
  26. }
  27.  
  28. public void onClick(View v) {
  29.     /*
  30.      * Create/Open a SQLite database and fill with dummy content and close
  31.      * it
  32.      */
  33.     mySQLiteAdapter = new SQLiteAdapter(this);
  34.     mySQLiteAdapter.openToWrite();
  35.     mySQLiteAdapter.deleteAll();
  36.     String s = edt.getText().toString();
  37.     mySQLiteAdapter.insert(s);
  38.     mySQLiteAdapter.close();
  39.  
  40.     /*
  41.      * Open the same SQLite database and read all it's content.
  42.      */
  43.     mySQLiteAdapter = new SQLiteAdapter(this);
  44.     mySQLiteAdapter.openToRead();
  45.     String contentRead = mySQLiteAdapter.queueAll();
  46.     mySQLiteAdapter.close();
  47.  
  48.     listContent.setText(contentRead);
  49.  
  50. }
  51.    
  52. public class SQLiteAdapter {
  53.  
  54. public static final String MYDATABASE_NAME = "MY_DATABASE";
  55. public static final String MYDATABASE_TABLE = "MY_TABLE";
  56. public static final int MYDATABASE_VERSION = 1;
  57. public static final String KEY_CONTENT = "Content";
  58.  
  59. // create table MY_DATABASE (ID integer primary key, Content text not null);
  60. private static final String SCRIPT_CREATE_DATABASE = "create table "
  61.         + MYDATABASE_TABLE + " (" + KEY_CONTENT + " text not null);";
  62.  
  63. private SQLiteHelper sqLiteHelper;
  64. private SQLiteDatabase sqLiteDatabase;
  65.  
  66. private Context context;
  67.  
  68. public SQLiteAdapter(Context c) {
  69.     context = c;
  70. }
  71.  
  72. public SQLiteAdapter openToRead() throws android.database.SQLException {
  73.     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
  74.             MYDATABASE_VERSION);
  75.     sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  76.     return this;
  77. }
  78.  
  79. public SQLiteAdapter openToWrite() throws android.database.SQLException {
  80.     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
  81.             MYDATABASE_VERSION);
  82.     sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  83.     return this;
  84. }
  85.  
  86. public void close() {
  87.     sqLiteHelper.close();
  88. }
  89.  
  90. public long insert(String content) {
  91.  
  92.     ContentValues contentValues = new ContentValues();
  93.     contentValues.put(KEY_CONTENT, content);
  94.     return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
  95. }
  96.  
  97. public int deleteAll() {
  98.     return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
  99. }
  100.  
  101. public String queueAll() {
  102.     String[] columns = new String[] { KEY_CONTENT };
  103.     Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
  104.             null, null, null, null);
  105.     String result = "";
  106.  
  107.     int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
  108.     for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
  109.         result = result + cursor.getString(index_CONTENT) + "n";
  110.     }
  111.  
  112.     return result;
  113. }
  114.  
  115. public class SQLiteHelper extends SQLiteOpenHelper {
  116.  
  117.     public SQLiteHelper(Context context, String name,
  118.             CursorFactory factory, int version) {
  119.         super(context, name, factory, version);
  120.     }
  121.  
  122.     @Override
  123.     public void onCreate(SQLiteDatabase db) {
  124.         // TODO Auto-generated method stub
  125.         db.execSQL(SCRIPT_CREATE_DATABASE);
  126.     }
  127.  
  128.     @Override
  129.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  130.         // TODO Auto-generated method stub
  131.  
  132.     }
  133.  
  134. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top