I have a RecyclerView
that creates CardView
s from user input (via two EditTexts).
I am able to create the new Cards properly. The first
With Activity.setResult(int resultCode, Intent data), the first parameter is the result code not the request code.
You're using setResult(1, intent);
its similar to setResult(Activity.RESULT_FIRST_USER, intent);
.
Note that:
RESULT_FIRST_USER : Start of user-defined 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 CardViewActivity
code, data.getStringExtra("MESSAGE2")
can return null object.
Looking at your code I see a problem with implementation of addItem
method here:
mItems.add(new ContactItem(contact));
notifyItemInserted(0);
It seems you are adding item on bottom and notify insertion on top. You should keep insertion and notification aligned in terms of position. In particular, if you want to insert on top you should change your code as follows:
mItems.add(0, new ContactItem(contact));
notifyItemInserted(0);