Toast receives intent data, why CardView in RecyclerView?

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 { // 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); } 

}

+1
source share
1 answer

Too many complications. Let's make it simple. The problem, it seems, in onActivityResult is to move the data to the list.

Remember to change the snippet as shown below.

Initially declare the user list and user adapter globally. (In your case: contact)

 AdapterContactList conAdapter; List<Contact> conList; 

In onCreate, initialize your list and add an empty list to the adapter and attach it to the RecyclerView.

 conList= new ArrayList<>(); conAdapter= new AdapterContactList (this, conList); recyclerView.setAdapter(conAdapter); 

Then in your onActivityResult do this as shown below.

 if (resultCode == RESULT_OK && requestCode == 101) { String doMessage = data.getStringExtra("MESSAGE1"); String doMessage1 = data.getStringExtra("MESSAGE2"); Contact con = new Contact(doMessage, doMessage1); conList.add(con); conAdapter.notifyDataSetChanged(); } 

What is it, now you will see that the data is correctly populated in the recycler view. And I hope your Recycliewiew adapter and the rest of the part work fine in this case.

Inquiries let me know.

0
source

Source: https://habr.com/ru/post/1242281/


All Articles