-1

I have n number of list of items, containing name and position. If I delete a row from list, I want to re-arrange the positions of the list.

For example:

Vijay  01
Dhas   02
Arun   03
prabhu 04

If I delete Dhas from the list, Arun's position should be 02 and prabhu's position should be 03.

My model class is

public class Model {

    private String name;
    private int position;

    public Model(String name, int position) {
        this.name = name;
        this.position = position;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

}

I tried the following:

 @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            holder.imvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    int tempPreviousValue;
                    for(int i = position ; i < mData.size()-1 ; i++){
                        int deletedPosition = mData.get(i).getPosition();
                        tempPreviousValue = mData.get(position+1).getPosition();
                        mData.get(position+1).setPosition(deletedPosition);
                    }
                    mData.remove(position);
                    notifyDataSetChanged();


                }
            }); 
        }

But I am not getting the correct order. Can anyone give some ideas to re-arrange the array?

Vijay Dhas
  • 71
  • 6
  • Just remove it in activity/fragment/anywhere you fill that list, not in adapter, and there call `adapter.notifyDataSetChanged();` – grabarz121 Jan 31 '19 at 07:49
  • after you delete item from the list, you can just set the `item.setPosition(list.indexOf(item))` to the position of the item in the list – Vladyslav Matviienko Jan 31 '19 at 07:49
  • If you delete item from List it will automatically update the positions and you don't need to do anything. What exactly you trying to achieve? – Maksim Novikov Jan 31 '19 at 07:50
  • @MaksimNovikov List position will be changed. But in my model position I need to change. – Vijay Dhas Jan 31 '19 at 07:53
  • @VijayDhas what is the purpose of position in the model? – Maksim Novikov Jan 31 '19 at 07:55
  • @VijayDhas you can use sortedList, for more info check out this link https://stackoverflow.com/questions/8725387/why-is-there-no-sortedlist-in-java – Hamid Reza Jan 31 '19 at 07:58
  • I am having position in model. I will get it from server. For example seat arrangement I am having 10 seats. 1st seat allocated to Vijay, 2nd seat allocated to Dhas, like wise 10 seats allocated. If I delete 5th seat person 6th seat person should allocated to seat 5 and need to change up to 10 seats. – Vijay Dhas Jan 31 '19 at 07:58
  • @HamidReza.. I no need to sort the list. I want to swap the value of position in model. – Vijay Dhas Jan 31 '19 at 08:00

7 Answers7

2

I actually recommend just using a plain list of names, and then relying on List#indexOf to find the positions.

String[] names = new String[] { "Vijay", "Dhas", "Arun", "prabhu" };
List<String> list = Arrays.asList(names);
list.remove("Dhas");
System.out.println(list.indexOf("Arun"));  // should be 1, was 2 before deletion

You may also use List<Model>, and the same logic applies. I am advocating that you let the list API maintain the position number for you, which it already does.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I can not use plain list. I am having Model class. I need to do change in model class. – Vijay Dhas Jan 31 '19 at 07:54
  • 2
    Doesn't change my answer. You may replace `String` with `Model` in my answer. The list API already does the work of maintaining positions, and you should use it. – Tim Biegeleisen Jan 31 '19 at 08:02
  • I am having position in model. I will get it from server. For example seat arrangement I am having 10 seats. 1st seat allocated to Vijay, 2nd seat allocated to Dhas, like wise 10 seats allocated. If I delete 5th seat person 6th seat person should allocated to seat 5 and need to change up to 10 seats. So I want to change in Model. Not in List. – Vijay Dhas Jan 31 '19 at 08:06
  • ...but to change model, you must change your data source, which is your list. – grabarz121 Jan 31 '19 at 09:10
0

Just make an loop, and use increment index as new position like this

    mData.remove(position);
    for (int i = 0; i < mData.size(); i++) {
        mData.get(i).setPosition(i);
    }
    notifyDataSetChanged();

Or if your position in model starts from numer 1, use .setPosition(i+1).
One thing, don't use position in recyclerview adapter, because this may have a wrong number, use holder.getAdapterPosition(); instead.

grabarz121
  • 616
  • 5
  • 13
  • I am having position in model. I will get it from server. For example seat arrangement I am having 10 seats. 1st seat allocated to Vijay, 2nd seat allocated to Dhas, like wise 10 seats allocated. If I delete 5th seat person 6th seat person should allocated to seat 5 and need to change up to 10 seats. – Vijay Dhas Jan 31 '19 at 08:05
  • Above code will do this, but do it outside an adapter, let change main source, not source in adapter. For clarity, you should remove one of position on a main data source too, not just in adapter. – grabarz121 Jan 31 '19 at 08:08
  • i will be the mdata postion. I want to update the mdata.setPostion with previous value. Simply said want to swap the values in sequence . – Vijay Dhas Jan 31 '19 at 08:10
  • 1
    @grabarz121 Why you add model after removing ? – Abhay Koradiya Jan 31 '19 at 08:23
  • And I think you really don't know, what you'll want to achieve. – grabarz121 Jan 31 '19 at 09:07
0

Maybe something like this:

int positionToDelete = 2;
Model modelToDelete = modelList.get(positionToDelete - 1);
modelList.remove(modelToDelete);
for(int i = positionToDelete - 1; i < modelList.size(); i++){
  Model modelToChange = modelList.get(i);
  modelToChange.setPosition(modelToChange.getPosition() - 1);
}

The list will be this before deleting:

[{name = Vijay, position = 1},
 {name = Dhas, position = 2},
 {name = Arun, position = 3},
 {name = prabhu, position = 4}]

And this after deleting:

[{name = Vijay, position = 1},
 {name = Arun, position = 2},
 {name = prabhu, position = 3}]

Try it online.

Or if you already know the Model you want to delete, the first two lines are this instead:

Model modelToDelete = ...; // Model retrieved from some place
int positionToDelete = modelToDelete.getPosition();

The rest stays the same.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • Thanks Kavin. Works good. Temporally I fixed this as solution. But If i receive input like this new Model("Item 1",12), new Model("Item 2",10), new Model("Item 3",12), new Model("Item 4",44), new Model("Item 5",35), new Model("Item 6",99), new Model("Item 7",54)), we will be in trouble. Is there any way to solve this? For this only I try to swap the values. Please let me know. – Vijay Dhas Jan 31 '19 at 10:50
  • @VijayDhas About that example you give. When we remove the model `Item 4` (position `44`), what will it become? Will it becomes `[{Item 1, 12}, {Item 2, 10}, {Item 3, 12}, {Item 5, 44}, {Item 6, 35}, {item 7, 99}]`, or will the items first be sorted on position, so it will becomes `[{Item 2, 10}, {Item 1, 12}, {Item 3, 12}, {Item 5, 35}, {Item 7, 44}, {Item 6, 54}]` when deleting position `44`? – Kevin Cruijssen Jan 31 '19 at 13:06
  • @VijayDhas ? What do you mean last value also removed? I was asking what the Model-list should become when you remove the `{"Item 4",44}`-Model. – Kevin Cruijssen Feb 01 '19 at 13:14
  • Older list: `{Item 1",12}, {Item 2",10}, {Item 3",12}, {Item 4",44}, {Item 5",35}, {Item 6",99}, {Item 7",54}` After remove itme 2 `{Item 1",12}, {Item 3",10}, {Item 4",12}, {Item 5",44}, {Item 6",35}, {Item 7",99}` Item 2 value 10 is swap to item 3, Item 3 value 12 is swap to item 4. like wise all the value will be swaped to end. So last value will be removed. In this example 54 is removed. That’s why I mentioned last position will be deleted. – Vijay Dhas Feb 05 '19 at 06:44
0

you just need to refresh Adapter with notifyDataSetChanged(); and write code like that :-

@Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            mData.get(position).setPosition(position);
        }

also add one more method for remove item in adapter.

public void remove_item(int position) {
       mData.remove(position);
        notifyDataSetChanged();
    }
Geet Thakur
  • 1,966
  • 1
  • 16
  • 23
0
       @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {
            holder.imvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int i=mData.size()-1;i>position;i--){
                        mData.get(i).setPosition(mData.get(i-1).getPosition());
                    }
                    mData.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
Vinil Chandran
  • 1,563
  • 1
  • 19
  • 33
0

Create a method something like this

public void removeItem(int position){
    // just a safe check
    if(position >= getItemCount() ){
        return;
    }

    mData.remove(position);
    notifyItemRemoved(position);
}

then call and pass the position you wanted to be removed.

johnguild
  • 435
  • 1
  • 5
  • 25
0

Here's one using Java8 streams:

List<String> l2 = list.stream().filter(a -> !a.equals("Dhas")).collect(Collectors.toList());

Note that this would create a new list.

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44