I have user activity input that passes EditText data back to the RecyclerView CardViews list. Everything is working fine, except for one problem. After I created the first CardView in the list, the next created CardView is not updated with user input lines. The second CardView shows the same rows of data from the first input (on the first CardView). For example, if I enter the word "apple" in the EditText line, then the first CardView displays the "apple" correctly. Later, I return to the input activity, and then enter the word "green" and a second CardView will be created, but it will again display the word "apple" instead of the expected "green".
I set the toasts when CardView is created, and they show the correct data obtained in the onActivityResult method (this means that the first data entry correctly shows the word "apple" for the first toasts and the second data entry "green" shows the second set of toasts). Therefore, when the toasts receive the data correctly, something may not work with the RecyclerView adapter, which is not updating properly. Any ideas?
CardViewActivity input file:
... public void onClickSave(View v) { ... 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 returnIntent2 = new Intent(); returnIntent2.putExtra("MESSAGE1", doMessage); returnIntent2.putExtra("MESSAGE2", note1Message); boolean success = true; if (success) { setResult(ListContactsActivity.RESULT_OK, returnIntent2); } else { setResult(ListContactsActivity.RESULT_CANCELED, returnIntent2); } finish(); }
RecyclerViewActivity File:
... @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); if((requestCode==1)&&(resultCode==RESULT_OK)&&(data !=null)){ String doMessage=data.getStringExtra("MESSAGE1"); String note1Message=data.getStringExtra("MESSAGE2"); Contact contact = new Contact(doMessage,note1Message); mContactsAdapter.addItem(contact); mRecyclerView.scrollToPosition(0); Toast toast2 = Toast.makeText(this, doMessage, Toast.LENGTH_LONG); toast2.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP,0,0); toast2.show(); Toast toast3 = Toast.makeText(this, note1Message, Toast.LENGTH_LONG); toast3.setGravity(Gravity.CENTER_VERTICAL | Gravity.BOTTOM,0,0); toast3.show(); } }
Adapter file:
public class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static class EmptyViewHolder extends RecyclerView.ViewHolder { public EmptyViewHolder(View itemView) { super(itemView); } } private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView cardBlankText2; public ContactViewHolder(View itemView) { super(itemView); cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2); itemView.setOnClickListener(this); } @Override public void onClick(View v) { if (mOnItemTapListener != null) { Contact contact = mContacts.get(getLayoutPosition()); mOnItemTapListener.onItemTap(contact, getLayoutPosition()); } } } private Context mContext; private LayoutInflater mLayoutInflater; private List<Contact> mContacts; private List<ListItem> mItems; private OnItemTapListener mOnItemTapListener; public ListContactsAdapter(Context context, List<Contact> contacts) { mContext = context; mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mContacts = contacts; mItems = buildItemsList(); } public void setOnItemTapListener(OnItemTapListener listener) { mOnItemTapListener = listener; } private List<ListItem> buildItemsList() { List<ListItem> items = new ArrayList<>(); if (mContacts.size() > 0) { for (Contact contact : mContacts) { items.add(new ContactItem(contact)); } } else {
}