0% found this document useful (0 votes)
6 views5 pages

Practical No 19

The document contains an Android application setup with an AndroidManifest.xml file defining the app's main and second activities, and an activity_main.xml layout featuring buttons to insert and fetch student data. The MainActivity.java class implements functionality to insert student records into a content provider and retrieve them, logging the results. The app is designed to manage student information using a content provider architecture.
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)
6 views5 pages

Practical No 19

The document contains an Android application setup with an AndroidManifest.xml file defining the app's main and second activities, and an activity_main.xml layout featuring buttons to insert and fetch student data. The MainActivity.java class implements functionality to insert student records into a content provider and retrieve them, logging the results. The app is designed to manage student information using a content provider architecture.
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/ 5

Practical No : 19

Name:Vikram Santosh Nimbalkar


Roll No:66
Input:
AndroidManifest.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.Practical_18"
tools:targetApi="31">
<activity android:name=".SecondActivity"/>

<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>
</activity>
</application>

</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

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

<Button
android:id="@+id/fetchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fetch Students"
android:layout_marginTop="10dp"/>
</LinearLayout>

ActivityMain.java
package com.example.contentproviderdemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "ContentProviderDemo";

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

Button insertButton = findViewById(R.id.insertButton);


Button fetchButton = findViewById(R.id.fetchButton);

// Insert student when button is clicked


insertButton.setOnClickListener(v -> insertStudent());

// Fetch students when button is clicked


fetchButton.setOnClickListener(v -> fetchStudents());
}

private void insertStudent() {


ContentValues values = new ContentValues();
values.put("name", "Anjay");
values.put("age", 20);
values.put("name", "Vijay");
values.put("age", 20);

Uri newUri = getContentResolver().insert(MyContentProvider.CONTENT_URI, values);


if (newUri != null) {
Log.d(TAG, "Inserted Student at: " + newUri);
} else {
Log.d(TAG, "Student already exists, not inserted.");
}
}

private void fetchStudents() {


Cursor cursor = getContentResolver().query(MyContentProvider.CONTENT_URI, null, null,
null, null);

if (cursor != null) {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
int age = cursor.getInt(cursor.getColumnIndexOrThrow("age"));

Log.d(TAG, "Student: ID=" + id + ", Name=" + name + ", Age=" + age);


}
cursor.close();
}
}
}

Output:

You might also like