Search code examples
androidandroid-sqliteandroid-recyclerview

Why recyclerview is invisible ,gone or disappear whenever adapter call notifyDataSetChanged?


Recyclerview is showing a data of sqlite database. Recyclerview is gone, invisible or i don't know what happen whenever data is inserted into the sqlite database.

Activity Code:

MessageListRV = (RecyclerView) findViewById(R.id.message_list_sender_list);
        myLastDbHandler=new MyDbHandler(MessageList.this,LAST_MESSAGE+".db",null,1);
        messageListAdapter=new MessageListAdapter(myLastDbHandler.sqlLastMessagesList(),MessageListRV,this,myLastDbHandler);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        MessageListRV.setLayoutManager(linearLayoutManager);
        MessageListRV.setAdapter(messageListAdapter);
refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                 messageListAdapter.updateData(myLastDbHandler.sqlMessagesList());
            }
        });

Adapter Code:

public void updateData(List<SqlMessages> sqlMessages)
    {
        this.userMessagesList=sqlMessages;
        notifyDataSetChanged();
    }

Solution

  • Reinstantiating the collection on an adapter doesnt work you have to modify the collection

    public void updateData(List<SqlMessages> sqlMessages)
        {
            if (userMessagesList.size > 0) userMessagesList.clear();
            userMessagesList.addAllsqlMessages),
            notifyDataSetChanged();
        }