Home > Blockchain >  How to update previous Items in Android Recycler view?
How to update previous Items in Android Recycler view?

Time:02-06

I have a RecyclerView to list a set of data. And on clicking each item , I have validation to check previous item is entered or not. If that item is not entered I want to enable an inline error (which is hidden in normal case) message in the previous row. I have done the scenario as shown below but error is showing only in the current row. Anyone suggest how I can enable/update previous row or a specific row.

public  boolean _validateListItems(int itemIndex)
    {
        int previousItemIndex = itemIndex - 1;


        for (int i = 0; i <= previousItemIndex; i  )
        {

            if ((listRecyclerItem.get(i).getEnable()==0))
            {

              return false;
            }
        }
return true;
    }


    holder.expand_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position>0){
                if(_validateListItems(position))
                {
                    
                    mExpandedPosition = isExpanded ? -1:position;
                    notifyItemChanged(previousExpandedPosition);
                    notifyItemChanged(position);
                    notifyDataSetChanged();

                }
                else
                 {

                    

                        holder.error.setVisibility(View.VISIBLE);
                        holder.error.setTextColor(ContextCompat.getColor(context, R.color.error_red));
                    

                } 


                }

            }
        });

CodePudding user response:

First of all RecyclerView is something that recycle view. View is generated is based on Data Model.

So, lets store the user button/checkbox click/checked action) to the respective Model/Item. To run the validation, get the items from the Adapter and check your conditions in Activity/Fragment [Looping is an expensive operation, use Coroutine or RxJava]. Execute your validation and if Validation is true for an item, just update the Item from the list and finally update the Adapter. You can pass the Error message in the item and render it to the View. And finally, must use DiffUtil to update the items in adapter.

CodePudding user response:

Declare an interface for your adapter that has callback to your viewModel and when each item changed you can return the call back ito viewmodel and store it in an object or array for example :

interface Callback { void onDataChanged(int itemPosition); }

call that method in onBindViewHolder when your item text changed

and in view model add the returned item into a list when you clicked on a button, you can check the items if your necessary item didn't exist you can return error

  • Related