Prévia do material em texto
Universidade Veiga de Almeida Programação de dispositivos móveis AVA1 Prof. Claudio Fico Fonseca Nova Iguaçu, Rio de Janeiro 2024.1 Vou assumir que escolhemos o objeto "Livro" para fins de exemplo. Aqui está uma implementação básica que atende aos requisitos mencionados: 1.Definindo a classe Livro: import android.content.ContentValues; public class Book { private int id; private String title; private String author; private int year; private double price; private String genre; public Book(int id, String title, String author, int year, double price, String genre) { this.id = id; this.title = title; this.author = author; this.year = year; this.price = price; this.genre = genre; } // Getters and Setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } @Override public String toString() { return title + " by " + author; } } 2.Criando DBHelper para o acesso a dados: import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.content.ContentValues; import android.database.Cursor; import java.util.ArrayList; import java.util.List; public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "library.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_BOOKS = "books"; private static final String COLUMN_ID = "id"; private static final String COLUMN_TITLE = "title"; private static final String COLUMN_AUTHOR = "author"; private static final String COLUMN_YEAR = "year"; private static final String COLUMN_PRICE = "price"; private static final String COLUMN_GENRE = "genre"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String createTableQuery = "CREATE TABLE " + TABLE_BOOKS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_TITLE + " TEXT, " + COLUMN_AUTHOR + " TEXT, " + COLUMN_YEAR + " INTEGER, " + COLUMN_PRICE + " REAL, " + COLUMN_GENRE + " TEXT" + ")"; db.execSQL(createTableQuery); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKS); onCreate(db); } // CRUD Operations public void addBook(Book book) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, book.getTitle()); values.put(COLUMN_AUTHOR, book.getAuthor()); values.put(COLUMN_YEAR, book.getYear()); values.put(COLUMN_PRICE, book.getPrice()); values.put(COLUMN_GENRE, book.getGenre()); db.insert(TABLE_BOOKS, null, values); db.close(); } public Book getBook(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_BOOKS, new String[]{COLUMN_ID, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_YEAR, COLUMN_PRICE, COLUMN_GENRE}, COLUMN_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Book book = new Book( cursor.getInt(cursor.getColumnIndex(COLUMN_ID)), cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)), cursor.getString(cursor.getColumnIndex(COLUMN_AUTHOR)), cursor.getInt(cursor.getColumnIndex(COLUMN_YEAR)), cursor.getDouble(cursor.getColumnIndex(COLUMN_PRICE)), cursor.getString(cursor.getColumnIndex(COLUMN_GENRE))); cursor.close(); db.close(); return book; } public List getAllBooks() { List bookList = new ArrayList(); String selectQuery = "SELECT * FROM " + TABLE_BOOKS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { Book book = new Book( cursor.getInt(cursor.getColumnIndex(COLUMN_ID)), cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)), cursor.getString(cursor.getColumnIndex(COLUMN_AUTHOR)), cursor.getInt(cursor.getColumnIndex(COLUMN_YEAR)), cursor.getDouble(cursor.getColumnIndex(COLUMN_PRICE)), cursor.getString(cursor.getColumnIndex(COLUMN_GENRE))); bookList.add(book); } while (cursor.moveToNext()); } cursor.close(); db.close(); return bookList; } public int updateBook(Book book) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, book.getTitle()); values.put(COLUMN_AUTHOR, book.getAuthor()); values.put(COLUMN_YEAR, book.getYear()); values.put(COLUMN_PRICE, book.getPrice()); values.put(COLUMN_GENRE, book.getGenre()); return db.update(TABLE_BOOKS, values, COLUMN_ID + " = ?", new String[]{String.valueOf(book.getId())}); } public void deleteBook(Book book) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_BOOKS, COLUMN_ID + " = ?", new String[]{String.valueOf(book.getId())}); db.close(); } } 3.Banco de dados import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.content.ContentValues; import android.database.Cursor; import java.util.ArrayList; import java.util.List; public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "library.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_BOOKS = "books"; private static final String COLUMN_ID = "id"; private static final String COLUMN_TITLE = "title"; private static final String COLUMN_AUTHOR = "author"; private static final String COLUMN_YEAR = "year"; private static final String COLUMN_PRICE = "price"; private static final String COLUMN_GENRE = "genre"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String createTableQuery = "CREATE TABLE " + TABLE_BOOKS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_TITLE + " TEXT, " + COLUMN_AUTHOR + " TEXT, " + COLUMN_YEAR + " INTEGER, " + COLUMN_PRICE + " REAL, " + COLUMN_GENRE + " TEXT" + ")"; db.execSQL(createTableQuery); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKS); onCreate(db); } // CRUD Operations public void addBook(Book book) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, book.getTitle()); values.put(COLUMN_AUTHOR, book.getAuthor()); values.put(COLUMN_YEAR, book.getYear()); values.put(COLUMN_PRICE, book.getPrice()); values.put(COLUMN_GENRE, book.getGenre()); db.insert(TABLE_BOOKS, null, values); db.close(); } public Book getBook(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_BOOKS, new String[]{COLUMN_ID, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_YEAR, COLUMN_PRICE, COLUMN_GENRE}, COLUMN_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Book book = new Book( cursor.getInt(cursor.getColumnIndex(COLUMN_ID)), cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)), cursor.getString(cursor.getColumnIndex(COLUMN_AUTHOR)), cursor.getInt(cursor.getColumnIndex(COLUMN_YEAR)), cursor.getDouble(cursor.getColumnIndex(COLUMN_PRICE)), cursor.getString(cursor.getColumnIndex(COLUMN_GENRE))); cursor.close(); db.close(); return book; } public List getAllBooks() { List bookList = new ArrayList(); String selectQuery = "SELECT * FROM " + TABLE_BOOKS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()){ do { Book book = new Book( cursor.getInt(cursor.getColumnIndex(COLUMN_ID)), cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)), cursor.getString(cursor.getColumnIndex(COLUMN_AUTHOR)), cursor.getInt(cursor.getColumnIndex(COLUMN_YEAR)), cursor.getDouble(cursor.getColumnIndex(COLUMN_PRICE)), cursor.getString(cursor.getColumnIndex(COLUMN_GENRE))); bookList.add(book); } while (cursor.moveToNext()); } cursor.close(); db.close(); return bookList; } public int updateBook(Book book) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, book.getTitle()); values.put(COLUMN_AUTHOR, book.getAuthor()); values.put(COLUMN_YEAR, book.getYear()); values.put(COLUMN_PRICE, book.getPrice()); values.put(COLUMN_GENRE, book.getGenre()); return db.update(TABLE_BOOKS, values, COLUMN_ID + " = ?", new String[]{String.valueOf(book.getId())}); } public void deleteBook(Book book) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_BOOKS, COLUMN_ID + " = ?", new String[]{String.valueOf(book.getId())}); db.close(); } } 4. Main activity import android.content.Intent; 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 androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView listViewBooks; private EditText editTextTitle, editTextAuthor, editTextYear, editTextPrice, editTextGenre; private Button buttonAdd, buttonUpdate, buttonDelete, buttonClear; private List bookList; private ArrayAdapter bookAdapter; private DBHelper dbHelper; private Book selectedBook; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new DBHelper(this); listViewBooks = findViewById(R.id.listViewBooks); editTextTitle = findViewById(R.id.editTextTitle); editTextAuthor = findViewById(R.id.editTextAuthor); editTextYear = findViewById(R.id.editTextYear); editTextPrice = findViewById(R.id.editTextPrice); editTextGenre = findViewById(R.id.editTextGenre); buttonAdd = findViewById(R.id.buttonAdd); buttonUpdate = findViewById(R.id.buttonUpdate); buttonDelete = findViewById(R.id.buttonDelete); buttonClear = findViewById(R.id.buttonClear); bookList = new ArrayList(); bookAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, bookList); listViewBooks.setAdapter(bookAdapter); listViewBooks.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int position, long l) { selectedBook = bookList.get(position); editTextTitle.setText(selectedBook.getTitle()); editTextAuthor.setText(selectedBook.getAuthor()); editTextYear.setText(String.valueOf(selectedBook.getYear())); editTextPrice.setText(String.valueOf(selectedBook.getPrice())); editTextGenre.setText(selectedBook.getGenre()); } }); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String title = editTextTitle.getText().toString(); String author = editTextAuthor.getText().toString(); int year = Integer.parseInt(editTextYear.getText().toString()); double price = Double.parseDouble(editTextPrice.getText().toString()); String genre = editTextGenre.getText().toString(); Book book = new Book(0, title, author, year, price, genre); dbHelper.addBook(book); refreshBookList(); clearFields(); } }); buttonUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectedBook != null) { String title = editTextTitle.getText().toString(); String author = editTextAuthor.getText().toString(); int year = Integer.parseInt(editTextYear.getText().toString()); double price = Double.parseDouble(editTextPrice.getText().toString()); String genre = editTextGenre.getText().toString(); selectedBook.setTitle(title); selectedBook.setAuthor(author); selectedBook.setYear(year); selectedBook.setPrice(price); selectedBook.setGenre(genre); dbHelper.updateBook(selectedBook); refreshBookList(); clearFields(); selectedBook = null; } } }); buttonDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectedBook != null) { dbHelper.deleteBook(selectedBook); refreshBookList(); clearFields(); selectedBook = null; } } }); buttonClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { clearFields(); selectedBook = null; } }); refreshBookList(); } private void refreshBookList() { bookList.clear(); bookList.addAll(dbHelper.getAllBooks()); bookAdapter.notifyDataSetChanged(); } private void clearFields() { editTextTitle.setText(""); editTextAuthor.setText(""); editTextYear.setText(""); editTextPrice.setText(""); editTextGenre.setText(""); } } 5.Código da interface XML