-1

I am getting list of state from network call. and i am coping it to another list. now when i remove items from one List the same item is also deleting from other list too. i am unable to set filter because of this problem. please let me know what to.

here is my code.

private void getStates() {
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please Wait");
    progressDialog.show();

    RestApi restApi = RetroSingleton.getInstance().getRestApi();
    restApi.getStates()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<State2>() {
                @Override
                public void onCompleted() {
                    progressDialog.dismiss();
                }

                @Override
                public void onError(Throwable e) {
                    progressDialog.dismiss();
                    e.printStackTrace();
                }

                @Override
                public void onNext(State2 model) {
                    state2 = model;
                    Log.d(Const.TAG, "onNext: " + model.getState().size());
                    Log.d(Const.TAG, "onNext: " + state2.getState().size());

                    model.getState().remove(0); //here i remove item

                    Log.d(Const.TAG, "onNext: " + model.getState().size());
                    Log.d(Const.TAG, "onNext: " + state2.getState().size());

                   //now size is same! WHY?
                }
            });
}

this is my State2.java

public class State2 {

@SerializedName("error")
private Boolean error;
@SerializedName("state")
private List<State> state;

public Boolean getError() {
    return error;
}

public void setError(Boolean error) {
    this.error = error;
}

public List<State> getState() {
    return state;
}

public void setState(List<State> state) {
    this.state = state;
}


public class State {
    @SerializedName("id")
    private String id;
    @SerializedName("name")
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

}

}

any help would be appreciated. please let me know i just can't figure out Thanks

xbadal
  • 1,284
  • 2
  • 11
  • 24

2 Answers2

1

You're not copying the State2-object, you just assign it to another variable. state2 and model are pointing to the same object.

You could try this:

state2 = new State2();
state2.setState(new ArrayList<String>(model));
Tobi
  • 182
  • 8
1

Copy state2 model data using copy constructor and also pass only List model value in new list.

Chetan Pushpad
  • 345
  • 1
  • 9