This is for Android Studio in Java Code.
• Design an app to store contacts information.
• Main screen shows all names saved in database as a list.
• One icon allows the customer to add a new contact to the database. When this icon
clicked, a blank EditContact screen is displayed.
• Main screen also allows searching by entering key information into the
searching box. The list of contact information will be updated when
information is entered into the searching box.
• Use SearchView to let customer enter key information into.
• Use SearchView.setOnQueryTextListener to listen the text change in search box.
• By clicking an item in the contact list, another screen will display the detail
information of the selected contact.
• The Detail screen will display the detail information of the selected contact.
Two buttons/icons at the bottom of the screen allows customer to choose
to save or delete this contact information.
• When save button/icon is clicked, this contact will be added/updated and back to
Main screen.
• When delete button/icon is clicked, this contact will be deleted and back to Main
screen.
This app is ONLY meant to save names and phone numbers. It only uses Two Screens, MainActivity and DetailActivity, the latter lets you edit contacts.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Also, create a new class called DatabaseHelper.java to handle the SQLite database operations:
java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_PHONE_NUMBER = "phone_number";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NAME + " TEXT,"
+ COLUMN_PHONE_NUMBER + " TEXT"
+ ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_PHONE_NUMBER, contact.getPhoneNumber());
long id = db.insert(TABLE_NAME, null, values);
db.close();
return id;
}
public void updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_PHONE_NUMBER, contact.getPhoneNumber());
db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[]{String.valueOf(contact.getId())});
db.close();
}
public void deleteContact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
db.close();
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<>();
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(COLUMN_PHONE_NUMBER));
Contact contact = new Contact(id, name, phoneNumber);
contactList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contactList;
}
}
With these code implementations, you should be able to create an app in Android Studio that can store contact information, display them in a list, search for contacts by name, add new contacts, update existing contacts, and delete contacts.