Advertisement
GGGG2468

Content Provider 1

Jan 30th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. activity_main.xml
  2.  
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <AbsoluteLayout
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. xmlns:tools="http://schemas.android.com/tools"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context=".MainActivity"
  11. tools:ignore="Deprecated">
  12. <EditText
  13. android:id="@+id/idDate"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_x="16dp"
  17. android:layout_y="16dp"
  18. android:ems="10"
  19. android:hint="Enter today's date"/>
  20. <EditText
  21. android:id="@+id/idNote"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_x="16dp"
  25. android:layout_y="61dp"
  26. android:ems="10"
  27. android:hint="Enter note content"/>
  28. <Button
  29. android:id="@+id/idAdd"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_x="16dp"
  33. android:layout_y="106dp"
  34. android:text="Add Note"/>
  35. </AbsoluteLayout>
  36.  
  37.  
  38. MainActivity.java
  39.  
  40.  
  41. package com.example.program4a;
  42. import android.os.Bundle;
  43. import android.view.View;
  44. import android.widget.Toast;
  45. import android.widget.Button;
  46. import android.widget.EditText;
  47. import android.content.ContentValues;
  48. import androidx.appcompat.app.AppCompatActivity;
  49. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  50. EditText date, note;
  51. Button add;
  52. @Override
  53. protected void onCreate(Bundle b) {
  54. super.onCreate(b);
  55. setContentView(R.layout.activity_main);
  56. date = findViewById(R.id.idDate);
  57. note = findViewById(R.id.idNote);
  58. add = findViewById(R.id.idAdd);
  59. add.setOnClickListener(this);
  60. }
  61. @Override
  62. public void onClick(View v) {
  63. if(v.equals(add)){
  64. String dateText = date.getText().toString();
  65. String noteText = note.getText().toString();
  66. ContentValues cv = new ContentValues();
  67. cv.put("date", dateText);
  68. cv.put("note", noteText);
  69. getContentResolver().insert(NotesProvider.CONTENT_URI, cv);
  70. Toast.makeText(this, "Note added", Toast.LENGTH_SHORT).show();
  71. }
  72. }
  73. }
  74.  
  75.  
  76.  
  77.  
  78. NotesProvider.java
  79.  
  80. package com.example.program4a;
  81. import android.net.Uri;
  82. import android.database.Cursor;
  83. import android.content.ContentValues;
  84. import android.content.ContentProvider;
  85. import android.database.sqlite.SQLiteDatabase;
  86. public class NotesProvider extends ContentProvider {
  87. static String AUTHORITY = "com.example.notesprovider";
  88. static String BASE_PATH = "notes";
  89. static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
  90. ProviderDatabase dbHelper;
  91. SQLiteDatabase db;
  92. @Override
  93. public boolean onCreate() {
  94. dbHelper = new ProviderDatabase(getContext(), ProviderDatabase.DATABASE_NAME, null, 1);
  95. return (dbHelper == null) ? false : true;
  96. }
  97. @Override
  98. public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
  99. db = dbHelper.getReadableDatabase();
  100. Cursor c = db.query(ProviderDatabase.TABLE_NAME, strings, s, strings1, null, null, null);
  101. return c;
  102. }
  103. @Override
  104. public String getType(Uri uri) {
  105. return null;
  106. }
  107. @Override
  108. public Uri insert(Uri uri, ContentValues cv) {
  109. db = dbHelper.getWritableDatabase();
  110. db.insert(ProviderDatabase.TABLE_NAME, null, cv);
  111. db.close();
  112. return null;
  113. }
  114. @Override
  115. public int delete(Uri uri, String s, String[] strings) {
  116. return 0;
  117. }
  118. @Override
  119. public int update(Uri uri, ContentValues cv, String s, String[] strings) {
  120. return 0;
  121. }
  122. }
  123.  
  124.  
  125.  
  126. ProviderDatabase.java
  127.  
  128. package com.example.program4a;
  129. import android.content.Context;
  130. import android.database.sqlite.SQLiteDatabase;
  131. import android.database.sqlite.SQLiteOpenHelper;
  132. public class ProviderDatabase extends SQLiteOpenHelper {
  133. public static String DATABASE_NAME = "notes.db";
  134. public static String TABLE_NAME = "notes";
  135. public ProviderDatabase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  136. super(context, name, factory, version);
  137. }
  138. @Override
  139. public void onCreate(SQLiteDatabase db) {
  140. db.execSQL("CREATE TABLE " + TABLE_NAME + " (date TEXT, note TEXT);");
  141. }
  142. @Override
  143. public void onUpgrade(SQLiteDatabase db, int i, int i1) {
  144. }
  145. }
  146. <provider
  147. android:authorities="com.example.notesprovider"
  148. android:name=".NotesProvider"
  149. android:exported="true"
  150. android:enabled="true"/>
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement