This question already has an answer here:
- Android: why is CardView not updating with new user input? 2 answers
I have a user input Activity that passes EditText
data back to a RecyclerView
list of CardViews. Everything is working fine except for one problem. After I've created the first CardView
in the list, the next CardView created doesn't update with the user input data strings. The second CardView shows the same data strings from the first input (on the first CardView). For example, if I input the word "apple" on the EditText line then the first CardView shows "apple" correctly. Later I go back to the input Activity and then I input the word "green" and the second CardView will be created but it will show the word "apple" again instead of the expected "green".
I set up toasts for when the CardView is created and they show the correct data being received in the onActivityResult
method (meaning the first data input shows the word "apple" correctly for the first toasts and the second data input "green" shows for the second set of toasts). So while the toasts are receiving the data correctly perhaps something is not working with the RecyclerView's adapter not getting updated 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 {
// when R. list is first created or when the number of cards
// is deleted until there are zero, show the defaultcard_layout
// and "Click the + above to get started".
for (int i=0; i<1; i++) {
items.add(new EmptyItem());
}
}
return items;
}
public void addItem(Contact contact) {
if (mContacts.size()==0) {
// if list is empty we must
// remove empty cards first
mItems.clear();
notifyDataSetChanged();
}
// in any case we need to add
// item on the top of the list and scroll to the top position
mContacts.add(contact);
mItems.add(new ContactItem(contact));
notifyItemInserted(0);
}
public void removeItem(Contact contact, int position) {
mContacts.remove(contact);
if (mContacts.size()==0) {
// if no more contacts in list,
// we rebuild from scratch
mItems.clear();
mItems.addAll(buildItemsList());
notifyDataSetChanged();
} else {
// else we just need to remove
// one item
mItems.remove(position);
notifyItemRemoved(position);
}
}
@Override
public int getItemCount() {
return mItems.size();
}
@Override
public int getItemViewType(int position) {
return mItems.get(position).getType();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ListItem.EMPTY_TYPE) {
View itemView = mLayoutInflater.inflate(R.layout.defaultcard_layout, parent, false);
return new EmptyViewHolder(itemView);
} else {
View itemView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
return new ContactViewHolder(itemView);
}
}
@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());
}
}
public interface OnItemTapListener {
void onItemTap(Contact contact, int position);
}
}