Sure! Here is an example implementation of the app described above:
- activity_main.xml (layout file for main screen):
<ListView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<!-- Add other components as needed -->
- activity_edit_contact.xml (layout file for EditContact screen):
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edit_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<!-- Add other components as needed -->
- activity_contact_detail.xml (layout file for Detail screen):
<TextView
android:id="@+id/details_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/details_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<!-- Add other components as needed -->
- Contact class:
public class Contact {
private int id;
private String name;
private String phoneNumber;
// Add constructor, getters, and setters
}
- ContactsDatabaseHelper class:
public class ContactsDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION = 1;
// Add code to create and upgrade database
// Add methods to manipulate contacts in the database
}
- MainActivity:
public class MainActivity extends AppCompatActivity {
private ListView contactList;
private CursorAdapter cursorAdapter;
// Database helper instance
private ContactsDatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = findViewById(R.id.contact_list);
// Instantiate database helper
databaseHelper = new ContactsDatabaseHelper(this);
// Set up the cursor adapter
cursorAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
null,
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
new int[]{android.R.id.text1},
0
);
contactList.setAdapter(cursorAdapter);
// Set listener for add button/icon
FloatingActionButton addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, EditContactActivity.class);
startActivity(intent);
}
});
// Set up the SearchView
SearchView searchView = findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Handle search query submit
updateContactList(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// Handle search query text change
updateContactList(newText);
return true;
}
});
}
private void updateContactList(String query) {
// Update the contact list based on the search query
Cursor cursor = databaseHelper.getContacts(query);
cursorAdapter.changeCursor(cursor);
}
// Add other methods as needed
}
- EditContactActivity:
public class EditContactActivity extends AppCompatActivity {
private EditText editName;
private EditText editPhone;
// Add database helper instance
private ContactsDatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_contact);
editName = findViewById(R.id.edit_name);
editPhone = findViewById(R.id.edit_phone);
// Instantiate database helper
databaseHelper = new ContactsDatabaseHelper(this);
// Set listener for save button/icon
FloatingActionButton saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
String phone = editPhone.getText().toString();
// Create and save the new contact
Contact contact = new Contact();
contact.setName(name);
contact.setPhoneNumber(phone);
databaseHelper.saveContact(contact);
// Return to the main screen
Intent intent = new Intent(EditContactActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
// Add other methods as needed
}
- DetailActivity:
public class DetailActivity extends AppCompatActivity {
private TextView detailsName;
private TextView detailsPhone;
private int contactId;
// Add database helper instance
private ContactsDatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_detail);
detailsName = findViewById(R.id.details_name);
detailsPhone = findViewById(R.id.details_phone);
// Instantiate database helper
databaseHelper = new ContactsDatabaseHelper(this);
// Get the selected contact ID from the intent
contactId = getIntent().getIntExtra("contact_id", -1);
// Populate the details of the selected contact
Contact contact = databaseHelper.getContact(contactId);
detailsName.setText(contact.getName());
detailsPhone.setText(contact.getPhoneNumber());
// Set listener for save button/icon
FloatingActionButton saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = detailsName.getText().toString();
String phone = detailsPhone.getText().toString();
// Update the contact details
Contact contact = new Contact();
contact.setId(contactId);
contact.setName(name);
contact.setPhoneNumber(phone);
databaseHelper.updateContact(contact);
// Return to the main screen
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
startActivity(intent);
}
});
// Set listener for delete button/icon
FloatingActionButton deleteButton = findViewById(R.id.delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Delete the contact
databaseHelper.deleteContact(contactId);
// Return to the main screen
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
// Add other methods as needed
}
Remember to update the layout files and add any missing imports as needed. The code provided here is a general framework and may require adjustments based on your specific requirements.