0% found this document useful (0 votes)
54 views

Mad 19

This document contains code for an Android notes app that allows users to insert note titles and contents into a SQLite database via a content provider. The MainActivity class contains code to get user input from EditText fields and insert it into the database using the content resolver. The NotesProvider class implements the content provider to allow querying, inserting, updating, and deleting notes from the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Mad 19

This document contains code for an Android notes app that allows users to insert note titles and contents into a SQLite database via a content provider. The MainActivity class contains code to get user input from EditText fields and insert it into the database using the content resolver. The NotesProvider class implements the content provider to allow querying, inserting, updating, and deleting notes from the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Mad 19

Mainactivity.java
package com.example.practice;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText titleEditText;


private EditText contentEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize EditText fields


titleEditText = findViewById(R.id.titleEditText);
contentEditText = findViewById(R.id.contentEditText);

// Set OnClickListener for a button to insert data


Button insertButton = findViewById(R.id.insertButton);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertData();
}
});
}

private void insertData() {


// Get input data from EditText fields
String title = titleEditText.getText().toString().trim();
String content = contentEditText.getText().toString().trim();

// Prepare content values for insertion


ContentValues values = new ContentValues();
values.put(NotesContract.NoteEntry.COLUMN_TITLE, title);
values.put(NotesContract.NoteEntry.COLUMN_CONTENT, content);

// Insert data using the content resolver


ContentResolver contentResolver = getContentResolver();
Uri uri =
Uri.parse("content://com.example.yourpackagename.notesprovider/notes");
Uri insertedUri = contentResolver.insert(uri, values);

if (insertedUri != null) {
// Data inserted successfully
Toast.makeText(MainActivity.this, "Data inserted successfully",
Toast.LENGTH_SHORT).show();
} else {
// Failed to insert data
Toast.makeText(MainActivity.this, "Failed to insert data",
Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Title"
android:layout_marginBottom="16dp"/>

<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Content"
android:layout_below="@id/titleEditText"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Data"
android:layout_below="@id/contentEditText"/>

</RelativeLayout>

Content provider
package com.example.practice;

import com.example.practice.NotesContract;
import com.example.practice.NotesDbHelper;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class NotesProvider extends ContentProvider {


private NotesDbHelper dbHelper;
@Override
public boolean onCreate() {
dbHelper = new NotesDbHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(NotesContract.NoteEntry.TABLE_NAME,
projection, selection, selectionArgs,
null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
long id = db.insert(NotesContract.NoteEntry.TABLE_NAME, null,
values);
getContext().getContentResolver().notifyChange(uri, null);
return Uri.withAppendedPath(uri, String.valueOf(id));
}
@Override
public int delete(@NonNull Uri uri, @Nullable String s, @Nullable
String[] strings) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues
contentValues, @Nullable String s, @Nullable String[] strings) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy