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

Practical No 19

The document contains an Android application code that includes an XML layout file and a Java activity class. The layout features input fields for a name and phone number, along with buttons to insert and query contacts. The Java code handles inserting data into a content provider and querying existing contacts to display results in a TextView.

Uploaded by

Vaman Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Practical No 19

The document contains an Android application code that includes an XML layout file and a Java activity class. The layout features input fields for a name and phone number, along with buttons to insert and query contacts. The Java code handles inserting data into a content provider and querying existing contacts to display results in a TextView.

Uploaded by

Vaman Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exp.

19

#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:padding="16dp">

<EditText

android:id="@+id/nameEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Name" />

<EditText

android:id="@+id/phoneEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Phone" />

<Button

android:id="@+id/insertButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Insert Contact" />

<Button

android:id="@+id/queryButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Query Contacts" />

<TextView

android:id="@+id/resultTextView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Results will be displayed here"

android:paddingTop="16dp" />

</LinearLayout>

#MainActivity.java

package com.example.contentproviderdemo;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText nameEditText, phoneEditText;

private TextView resultTextView;

private static final Uri CONTENT_URI =


Uri.parse("content://com.example.contentproviderdemo.provider/contacts");

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

nameEditText = findViewById(R.id.nameEditText);

phoneEditText = findViewById(R.id.phoneEditText);

resultTextView = findViewById(R.id.resultTextView);

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

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

// Insert data into the content provider

insertButton.setOnClickListener(v -> {

String name = nameEditText.getText().toString();

String phone = phoneEditText.getText().toString();


ContentValues values = new ContentValues();

values.put(ContactsDatabaseHelper.COLUMN_NAME, name);

values.put(ContactsDatabaseHelper.COLUMN_PHONE, phone);

Uri newContactUri = getContentResolver().insert(CONTENT_URI, values);

resultTextView.setText("Inserted: " + newContactUri.toString());

});

// Query data from the content provider

queryButton.setOnClickListener(v -> {

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

StringBuilder result = new StringBuilder();

if (cursor != null && cursor.moveToFirst()) {

do {

String name =
cursor.getString(cursor.getColumnIndex(ContactsDatabaseHelper.COLUMN_NAME));

String phone =
cursor.getString(cursor.getColumnIndex(ContactsDatabaseHelper.COLUMN_PHONE));

result.append("Name: ").append(name).append(", Phone: ").append(phone).append("\n");

} while (cursor.moveToNext());

cursor.close();

resultTextView.setText(result.toString());

});

}
#output

You might also like