Android: why is CardView not updating with new user input?

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; } } 
0
source share
2 answers

Looking at your code, I see a problem using the addItem method here:

 mItems.add(new ContactItem(contact)); notifyItemInserted(0); 

It seems you are adding an element at the bottom and notifying of the inclusion at the top. You must keep the insert and notification in line with the position. In particular, if you want to paste on top, you must change your code as follows:

 mItems.add(0, new ContactItem(contact)); notifyItemInserted(0); 
+1
source

With Activity.setResult (int resultCode, Intent data) , the first parameter is the result code , not the request code .

Used setResult(1, intent); similar to setResult(Activity.RESULT_FIRST_USER, intent); .

Note that:

RESULT_FIRST_USER: start custom activity results.


Change your code to:

 setResult(Activity.RESULT_OK , intent); 

AND

 if( (requestCode==1) && (resultCode==Activity.RESULT_OK) ) { String doMessage=data.getStringExtra("MESSAGE1"); String note1Message=data.getStringExtra("MESSAGE2"); Contact contact = new Contact(doMessage,note1Message); mContactsAdapter.addItem(contact); //maybe you should call "mContactsAdapter.notifyItemInserted(...)" to refresh your adapter, if you didn't do that in addItem() method. mRecyclerView.scrollToPosition(0); } 

PS: From your code, CardViewActivity data.getStringExtra("MESSAGE2") can return a null object.

0
source

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


All Articles