How to save and return an ArrayList from my recycleview adapter after reorder it in recycleview.widget(xml)

27 views Asked by At

I have this code block on my MainActivity, which I have a recycleview and I am able to reorder a list, when I finish I have a button that finalize and save and return the reordered list in order to check if the items is in the right order

    private void initRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        RecyclerViewAdapter adapter = new RecyclerViewAdapter();
        adapter.setDataList(dataModelList,5);

        ItemTouchHelper.Callback callback = new RecyclerRowMoveCallback(adapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(recyclerView);

        recyclerView.setAdapter(adapter);


        // Button to finalize the positions
        Button finalizeButton = findViewById(R.id.finalize_button);
        finalizeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//
                SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();

                for (int i = 0; i < dataModelList.size(); i++) {
                    editor.putString("item_" + i, dataModelList.get(i).getTitle());
                }
                editor.apply();
                isTimerRunning = false;
                // Check if the list is in the right order
                boolean areInOrder = areItemsInOrder(savedList, dataModelList);
                // Display a message
                if (areInOrder) {Do something}
                else{Do something else}

and I have my adapter with all the necessary functions and this

    public ArrayList<DataModel> getItems() {
        return dataList;
    }

in order to return the reordered list.

But the reordered list every time is return in same order.

I include also here the algorithm that check the right order of (reordered)list1 according the order of list2.

public boolean areItemsInOrder(ArrayList<String> list1, ArrayList<DataModel> list2) {
        boolean found = true;
        int index = 0;
        for (String item : list1) {
            if (list2.contains(item)) {
                if (list2.indexOf(item) < index) {
                    found = false;
                    break;
                }
                index = list2.indexOf(item);
            } else {
                found = false;
                break;
            }
        }
        return found;
    }

Could you please help me to find the issue in here?

0

There are 0 answers