0% found this document useful (0 votes)
411 views10 pages

MAD Prac 19

The document describes an Android application that allows users to insert, display, and store book titles and authors. It includes code for the activity layout, activity class, content provider, and book model class. The content provider implements insert and query methods to add and retrieve book data from a list, which is displayed in the activity's list view.

Uploaded by

fandattejam
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)
411 views10 pages

MAD Prac 19

The document describes an Android application that allows users to insert, display, and store book titles and authors. It includes code for the activity layout, activity class, content provider, and book model class. The content provider implements insert and query methods to add and retrieve book data from a list, which is displayed in the activity's list view.

Uploaded by

fandattejam
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/ 10

Practical 19:

Exercise:
Program:
activity_main:
<?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"
tools:context=".MainActivity">

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

<EditText
android:id="@+id/editTextAuthor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Author"
android:layout_below="@id/editTextTitle"
android:layout_margin="16dp"
/>
<Button
android:id="@+id/buttonInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextAuthor"
android:layout_centerHorizontal="true"
android:text="Insert"
android:layout_marginTop="16dp"
/>

<ListView
android:id="@+id/listViewBooks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/buttonInsert"
android:layout_marginTop="16dp"
/>

</RelativeLayout>
MainActivity:
package com.example.mad;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private EditText editTextTitle;


private EditText editTextAuthor;
private Button buttonInsert;
private ListView listViewBooks;
private ArrayAdapter<String> adapter;
private ArrayList<String> bookList;

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

editTextTitle = findViewById(R.id.editTextTitle);
editTextAuthor = findViewById(R.id.editTextAuthor);
buttonInsert = findViewById(R.id.buttonInsert);
listViewBooks = findViewById(R.id.listViewBooks);

buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertBook();
}
});

bookList = new ArrayList<>();


adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, bookList);
listViewBooks.setAdapter(adapter);

// Load books initially


loadBooks();
}

private void insertBook() {


String title = editTextTitle.getText().toString().trim();
String author = editTextAuthor.getText().toString().trim();

if (title.isEmpty() || author.isEmpty()) {
Toast.makeText(this, "Please enter both title and author",
Toast.LENGTH_SHORT).show();
return;
}

ContentValues values = new ContentValues();


values.put("title", title);
values.put("author", author);

Uri uri = Uri.parse("content://com.example.mad.provider/books");


Uri insertedUri = getContentResolver().insert(uri, values);
if (insertedUri != null) {
Toast.makeText(this, "Book inserted successfully", Toast.LENGTH_SHORT).show();
// Clear input fields after insertion
editTextTitle.getText().clear();
editTextAuthor.getText().clear();
// Reload books after insertion
loadBooks();
} else {
Toast.makeText(this, "Failed to insert book", Toast.LENGTH_SHORT).show();
}
}

private void loadBooks() {


bookList.clear();
Uri uri = Uri.parse("content://com.example.mad.provider/books");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor.getColumnIndex("title"));
String author = cursor.getString(cursor.getColumnIndex("author"));
bookList.add(title + " by " + author);
} while (cursor.moveToNext());
cursor.close();
}
adapter.notifyDataSetChanged();
}
}
BookProvider:
package com.example.mad;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class BookProvider extends ContentProvider {

private static final int BOOKS = 1;

private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

private List<Book> books;

static {
uriMatcher.addURI("com.example.mad.provider", "books", BOOKS);
}

@Override
public boolean onCreate() {
books = new ArrayList<>();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "title", "author"});

switch (uriMatcher.match(uri)) {
case BOOKS:
for (Book book : books) {
cursor.addRow(new Object[]{book.getId(), book.getTitle(), book.getAuthor()});
}
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}

return cursor;
}

@Override
public String getType(Uri uri) {
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
switch (uriMatcher.match(uri)) {
case BOOKS:
String title = values.getAsString("title");
String author = values.getAsString("author");
long id = System.currentTimeMillis(); // Using current time as ID for simplicity
Book book = new Book(id, title, author);
books.add(book);
return Uri.withAppendedPath(uri, String.valueOf(id));
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
throw new UnsupportedOperationException("Not yet implemented"); }

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("Not yet implemented");
}
}

Book:
package com.example.mad;

public class Book {


private long id;
private String title;
private String author;
public Book(long id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}

public long getId() {


return id;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}
}
Manifest:
<provider
android:name=".BookProvider"
android:authorities="com.example.mad.provider"
android:exported="true" />
Output:

You might also like