Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

This is for Android Studio in Java Code. • Design an app to store contacts information.…

  1. User

    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.

  2. Assistant

    Sure! Here's an example code for the MainActivity class:

    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ListView;
    import android.widget.SearchView;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    
        private ListView contactListView;
        private ContactAdapter contactAdapter;
        private DatabaseHelper databaseHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            contactListView = findViewById(R.id.contact_list_view);
            databaseHelper = new DatabaseHelper(this);
            
            // Populate contact list from database
            contactAdapter = new ContactAdapter(this, getContacts());
            contactListView.setAdapter(contactAdapter);
            
            // Initialize the search view
            SearchView searchView = findViewById(R.id.search_view);
            searchView.setOnQueryTextListener(this);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == R.id.add_contact) {
                Intent intent = new Intent(this, DetailActivity.class);
                startActivity(intent);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            // Refresh contact list when returning from DetailActivity
            contactAdapter.updateContacts(getContacts());
            contactAdapter.notifyDataSetChanged();
        }
    
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            String filterText = newText.toLowerCase().trim();
            List<Contact> filteredContacts = new ArrayList<>();
    
            for (Contact contact : getContacts()) {
                if (contact.getName().toLowerCase().contains(filterText) ||
                    contact.getPhoneNumber().toLowerCase().contains(filterText)) {
                    filteredContacts.add(contact);
                }
            }
    
            contactAdapter.updateContacts(filteredContacts);
            contactAdapter.notifyDataSetChanged();
    
            return true;
        }
    
        private List<Contact> getContacts() {
            List<Contact> contacts = new ArrayList<>();
            
            SQLiteDatabase db = databaseHelper.getReadableDatabase();
            
            Cursor cursor = db.query(DatabaseHelper.TABLE_CONTACTS,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null);
            
            while (cursor.moveToNext()) {
                int idIndex = cursor.getColumnIndex(DatabaseHelper.COL_ID);
                int nameIndex = cursor.getColumnIndex(DatabaseHelper.COL_NAME);
                int phoneNumberIndex = cursor.getColumnIndex(DatabaseHelper.COL_PHONE_NUMBER);
                
                int id = cursor.getInt(idIndex);
                String name = cursor.getString(nameIndex);
                String phoneNumber = cursor.getString(phoneNumberIndex);
                
                contacts.add(new Contact(id, name, phoneNumber));
            }
            
            cursor.close();
            db.close();
    
            return contacts;
        }
    }
    

    And here's an example code for the DetailActivity class:

    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class DetailActivity extends AppCompatActivity {
    
        private EditText nameEditText;
        private EditText phoneNumberEditText;
        private DatabaseHelper databaseHelper;
        private int contactId;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);
            
            nameEditText = findViewById(R.id.name_edit_text);
            phoneNumberEditText = findViewById(R.id.phone_number_edit_text);
            databaseHelper = new DatabaseHelper(this);
            
            Intent intent = getIntent();
            if (intent.hasExtra("contact_id")) {
                contactId = intent.getIntExtra("contact_id", -1);
                loadContact(contactId);
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.detail_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == R.id.save_contact) {
                saveContact();
                return true;
            } else if (item.getItemId() == R.id.delete_contact) {
                deleteContact();
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        private void loadContact(int contactId) {
            SQLiteDatabase db = databaseHelper.getReadableDatabase();
            
            String selection = DatabaseHelper.COL_ID + "=?";
            String[] selectionArgs = {String.valueOf(contactId)};
            
            Cursor cursor = db.query(DatabaseHelper.TABLE_CONTACTS,
                    null,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    null);
            
            if (cursor.moveToNext()) {
                int nameIndex = cursor.getColumnIndex(DatabaseHelper.COL_NAME);
                int phoneNumberIndex = cursor.getColumnIndex(DatabaseHelper.COL_PHONE_NUMBER);
                
                String name = cursor.getString(nameIndex);
                String phoneNumber = cursor.getString(phoneNumberIndex);
                
                nameEditText.setText(name);
                phoneNumberEditText.setText(phoneNumber);
            }
            
            cursor.close();
            db.close();
        }
    
        private void saveContact() {
            String name = nameEditText.getText().toString().trim();
            String phoneNumber = phoneNumberEditText.getText().toString().trim();
            
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phoneNumber)) {
                Toast.makeText(this, "Please enter name and phone number", Toast.LENGTH_SHORT).show();
                return;
            }
            
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            
            ContentValues values = new ContentValues();
            values.put(DatabaseHelper.COL_NAME, name);
            values.put(DatabaseHelper.COL_PHONE_NUMBER, phoneNumber);
    
            if (contactId != -1) {
                String selection = DatabaseHelper.COL_ID + "=?";
                String[] selectionArgs = {String.valueOf(contactId)};
                
                db.update(DatabaseHelper.TABLE_CONTACTS, values, selection, selectionArgs);
                Toast.makeText(this, "Contact updated", Toast.LENGTH_SHORT).show();
            } else {
                db.insert(DatabaseHelper.TABLE_CONTACTS, null, values);
                Toast.makeText(this, "Contact saved", Toast.LENGTH_SHORT).show();
            }
            
            db.close();
            finish();
        }
    
        private void deleteContact() {
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            
            String selection = DatabaseHelper.COL_ID + "=?";
            String[] selectionArgs = {String.valueOf(contactId)};
            
            db.delete(DatabaseHelper.TABLE_CONTACTS, selection, selectionArgs);
            
            Toast.makeText(this, "Contact deleted", Toast.LENGTH_SHORT).show();
    
            db.close();
            finish();
        }
    }
    

    You will also need to create a layout file for the MainActivity and DetailActivity, as well as a Contact class to represent the contact information. Remember to update the layout files accordingly and create a DatabaseHelper class to handle the SQLite database operations.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.