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?