Home > front end >  How updating data inside RecyclerView works?
How updating data inside RecyclerView works?

Time:02-22

As we all know java is call by value, and here we are passing a LinkedList to Adapter class. like this

// this snippet is belongs to mainActivity
private final LinkedList<String> mWordList = new LinkedList<>();
        createDataSet();   // Let's create a data set by calling this method
        mRecyclerView  = findViewById(R.id.recycler_view);
        // lets create an adapter
        mWordListAdapter = new WordListAdapter(this,mWordList);
        mRecyclerView.setAdapter(mWordListAdapter);     // connect the adapter to the view
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));    // pass a layout manager

And adapter class having it's own linkedList which get the data of linkedList just we passed from MainActivity.

// Adapter class
public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {
    private LinkedList<String> mwordList;
    private LayoutInflater mInflater;

    // get the data from mainActivity and the layout inflator
    public WordListAdapter(Context context, LinkedList<String> wordList) {
        mInflater = LayoutInflater.from(context);
        mwordList = wordList;
    }
// only showing up the needed snippets
}

And set an onclicklistner to modify list when click on an item like this

@Override
        public void onClick(View v) {
            // get the position of the item that was clicked
            int mPosition = getLayoutPosition();
            // use this position to get the affected item in the wordlist
            String element = mwordList.get(mPosition);
            // change the data in the wordList
            mwordList.set(mPosition,"cicked!" element);
            // notify the adapter that the data has been chagned
            // so recyclerVeiw can update it
            mWordListAdapter.notifyDataSetChanged();
        }

And there also a fab button in MainActivity to insert new data

binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override  // adding new word to the lsit
            public void onClick(View view) {
                int wordListSize = mWordList.size()   1;
                mWordList.add(" " "word" wordListSize);    // adding a new string element
                mRecyclerView.getAdapter().notifyItemInserted(wordListSize);  // notify dataset changed
                mRecyclerView.smoothScrollToPosition(wordListSize);   // scroll screen where we added new data
            }

Now my question is that how this data is changing without passing the updated LinkedList to adapter class, as we are only passing the size of updated list inside notifyItemIntserted. I want to know the background process because it's working mean something is there which went above my head.

CodePudding user response:

Its about reference . Your Adapter and Your Activity have same reference of the ArrayList so if u modify it, it will reflect in both .

public WordListAdapter(Context context, LinkedList<String> wordList) {
    mInflater = LayoutInflater.from(context);
    mwordList = wordList;
}

With this piece of code mwordList = wordList; you are assigning same Object to a new reference variable which is mwordList. Reference variable is different but The Object they are pointing to is same i.e the one you created inside main Activity.

For other Part if you do it like below and this is just for explaining the difference .

public WordListAdapter(Context context, LinkedList<String> wordList) {
        mInflater = LayoutInflater.from(context);
        mwordList = new LinkedList<>(wordList);
    }

This will not reflect the changes you made in the list you have in your Activity the reason being now both are pointing to a different object. We create a new object for mwordList with new keyword. I hope this make sense .

  • Related