Linearlayout: @override
Linearlayout: @override
android:layout_height="wrap_content"
android:text="Enter Date:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_date" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Note Content:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt_Content"
android:height="200dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Note"
android:id="@+id/btn_add_note" />
</LinearLayout>
MainActivity
package com.example.notesprovider;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText txtDate,txtContent;
Button btnAddNote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDate=(EditText)findViewById(R.id.txt_date);
txtContent=(EditText)findViewById(R.id.txt_Content);
btnAddNote=(Button)findViewById(R.id.btn_add_note);
btnAddNote.setOnClickListener(this);
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
//// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(btnAddNote))
{
String sdate=txtDate.getText().toString();
String scontent=txtContent.getText().toString();
ContentValues values = new ContentValues();
values.put("note_date",sdate);
values.put("content",scontent);
getContentResolver().insert(Uri.parse("content://com.example.notesprovider/
notes"),
values);
Toast.LENGTH_LONG).show();
}
}
}
NotesProvider
package com.example.notesprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
public class NotesProvider extends ContentProvider
{
ProviderDatabase(getContext(),ProviderDatabase.DATABASE_NAME+".db",null,1);
return (db == null)? false:true;
}
@Override
public Cursor query(Uri uri, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
Cursor cursor=null;
db = dbHelper.getReadableDatabase();
cursor=
db.query(ProviderDatabase.TABLE_NAME,arg1,arg2,arg3,arg4,null,null);
return cursor;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[]
arg3) {
// TODO Auto-generated method stub
return 0;
}
}
ProviderDatabase
package com.example.notesprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class ProviderDatabase extends SQLiteOpenHelper
{
public static String DATABASE_NAME="noteprovider";
public static String TABLE_NAME="notes";
public static String COLUMN_DATE="note_date";
public static String COLUMN_NOTE="content";
public ProviderDatabase(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table notes (note_date TEXT,content TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NotesProvider"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<provider android:name="NotesProvider"
android:authorities="com.example.notesprovider"
android:exported="true"/>
</application>
</manifest>