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

后端 未结 2 1435
旧巷少年郎
旧巷少年郎 2020-12-22 08:14

I have a RecyclerView that creates CardViews from user input (via two EditTexts).

I am able to create the new Cards properly. The first

相关标签:
2条回答
  • 2020-12-22 08:47

    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.

    0 讨论(0)
  • 2020-12-22 08:48

    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);
    
    0 讨论(0)
提交回复
热议问题