I have a RecyclerView that creates a CardView from user input (via two EditTexts).
I can correctly create new Maps. The first CardView shows two user input fields correctly. When I create a second CardView , it displays correctly on the layout, but the two data entry fields are incorrect. Instead of displaying a second set of two user inputs, the second CardView shows the same two user inputs from the first CardView .
Somehow, after creating the first CardView I need to clear String objects in the intentions that are set to onActivityResult() in the RecyclerView action. Then set the following user inputs to the next CardView . I donβt know why, but Android Studio also says that setSurname in the Contact class is never used, although it is used in the Adapter method onBindViewHolder .
Do I need to use the onStop() method to complete the onClickSave method and destroy CardViewActivity after the user enters data into the EditText fields? What am I missing here?
CardViewActivity file into which the user enters input:
... public void onClickSave(View v) { final int stringToDo = cListenerEditText.getText().toString().replace(" ", "").length(); // if only the two user input fields have data else if (stringToDo > 0 && stringNotes1 > 0 ) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(), 0); String doMessage = cListenerEditText.getText().toString(); String note1Message = dListenerEditText.getText().toString(); Intent intent = new Intent(); intent.putExtra("MESSAGE1", doMessage); intent.putExtra("MESSAGE2", note1Message); setResult(1, intent); finish(); } // If only stringToDo has data and others are empty. else if (stringToDo > 0 && stringNotes1 == 0 && stringNotes2 == 0 && stringDueDate == 0 && stringDueTime ==0) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(), 0); String doMessage = cListenerEditText.getText().toString(); Intent intent = new Intent(); intent.putExtra("MESSAGE1", doMessage); setResult(1, intent); finish(); }
RecyclerView Activity File:
... @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); if(requestCode==1) { String doMessage=data.getStringExtra("MESSAGE1"); String note1Message=data.getStringExtra("MESSAGE2"); Contact contact = new Contact(doMessage,note1Message); mContactsAdapter.addItem(contact); mRecyclerView.scrollToPosition(0); } }
Adapter:
... public class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { ... private List<ListItem> buildItemsList() { List<ListItem> items = new ArrayList<>(); if (mContacts.size() > 0) { for (Contact contact : mContacts) { items.add(new ContactItem(contact)); } } else { for (int i=0; i<1; i++) { items.add(new EmptyItem()); } } return items; } public void addItem(Contact contact) { if (mContacts.size()==0) { mItems.clear(); notifyDataSetChanged(); } mContacts.add(contact); mItems.add(new ContactItem(contact)); notifyItemInserted(0); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) { int type = getItemViewType(position); if (type == ListItem.CONTACT_TYPE) { ContactItem item = (ContactItem) mItems.get(position); final Contact contact = item.getContact(); ContactViewHolder holder = (ContactViewHolder) viewHolder; holder.cardBlankText2.setText(contact.getName() + " " + contact.getSurname()); } }
Data Model File:
public class Contact { private String name; private String surname; public Contact(String name, String surname) { this.name = name; this.surname = surname; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } }