Create a simple contacts app in Android Studio. It only saves names and phone numbers. -…
- User
Create a simple contacts app in Android Studio. It only saves names and phone numbers.
- The first screen should let me add, select or search for a contact.
- If I select an existing contact, it puts me into a Second Screen that should let me edit, save or delete the selected contact.
- Assistant
- User
it's not saving my contacts into the listview on the first screen. How do i fix that?
- Assistant
Apologies for the oversight. In order to fix the issue with saving contacts into the ListView on the first screen, you need to make a small modification in the
onActivityResultmethod of the MainActivity class.Replace the existing
onActivityResultmethod with the updated code shown below:@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == 1) { String name = data.getStringExtra("name"); String phone = data.getStringExtra("phone"); if (name != null && !name.isEmpty()) { if (phone == null) phone = ""; if (data.hasExtra("position")) { int position = data.getIntExtra("position", -1); contactList.set(position, name + "\n" + phone); } else { contactList.add(name + "\n" + phone); } adapter.notifyDataSetChanged(); } } }Now, when you add or edit a contact in the EditContactActivity and return back to the MainActivity, the contact will be properly added to the contact list and displayed in the ListView.
- User
Its still not working...
- Assistant
I apologize for the continuing issue. I have made an error in the previous response. To fix the problem, please make the following changes:
- Replace the
onItemClickmethod inMainActivitywith the updated code:
contactListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, EditContactActivity.class); intent.putExtra("contactName", contactList.get(position)); intent.putExtra("position", position); // Pass the position to EditContactActivity startActivityForResult(intent, 1); } }); - Replace the
- User
it still didnt work...
- Assistant
Apologies once again for the confusion. It seems there was an oversight in the previous response. In order to properly update the contacts list, you need to modify the
onActivityResultmethod inMainActivityas follows:@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == 1) { String name = data.getStringExtra("name"); String phone = data.getStringExtra("phone"); if (name != null && !name.isEmpty()) { if (phone == null) phone = ""; if (data.hasExtra("position")) { int position = data.getIntExtra("position", -1); contactList.set(position, name + "\n" + phone); } else { contactList.add(name + "\n" + phone); } adapter.notifyDataSetChanged(); } else if (data.hasExtra("position")) { int position = data.getIntExtra("position", -1); contactList.remove(position); adapter.notifyDataSetChanged(); } } } - User
the code resulted in my app crashing
- Assistant
I apologize for the inconvenience caused. To ensure the app functions correctly, please make the following changes:
- Replace the
onItemClickmethod inMainActivitywith the updated code:
contactListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String contactName = contactListView.getItemAtPosition(position).toString(); Intent intent = new Intent(MainActivity.this, EditContactActivity.class); intent.putExtra("contactName", contactName); intent.putExtra("position", position); startActivityForResult(intent, 1); } }); - Replace the
- User
replace searchEditText with searchview in MainActivity
- Assistant
Certainly! To replace the
EditTextwith aSearchViewinMainActivity, follow these steps:- Update the
activity_main.xmllayout file: Replace the existingEditTextwith aSearchView:
<SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:queryHint="Search Contact" android:iconifiedByDefault="false" /> - Update the
- User
can you show me the full java full with the searchview modification?
- Assistant