java


Removing Item in a List reflects other List of same Class. Why?


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
Copy state2 model data using copy constructor and also pass only List model value in new list.
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));

Related Links

Exception when trying to consume a soap web service
Getting value of a variable from 1 method to other in java
Convert char into KeyEvent on Android
How to send values one form to another form java with jframe(I checked “Passing values between JFrames” and i dont understand the answer) [duplicate]
LogstashEncoder utf-8 not working
Is it worth to use slf4j with log4j2
How to sync eclipse projects (using GitHub or any other methods) in a way that is accessible on different computers?
Java - read bit as boolean/write boolean as bit
Java Piped Input/Output Stream massive delay in communication
NetBeans w/ Gradle Support System Environments for Testing
Creating Multiple buttons on a Home page. Very new to this and dont know much. SignUp button results in the “application stopping”
Aggregate query not working in Kundera-Mongo
How to implements Event Issuer in Android?
Is there anything logically wrong with creating a JFrame then using dispose() and then another JFrame and so on
Compress inline images from .rtf file to a lower resolution
Trouble with dependencies in an OSGI bundle

Categories

HOME
comparison
appx
stock
vmware
mean-stack
c#-2.0
paradox
iggrid
tomcat6
php-7.1
apache-cayenne
ups
netflix
worldwind
progressive-web-apps
graphlab
windows-7-x64
angular-ui
google-pagespeed
jprofiler
dbext
object-detection
autoconf
mps
excel-2007
telerik-reporting
facebook-access-token
virtualdub
windows-error-reporting
fifo
elasticsearch-ruby
saas
phonegap
twilio-api
.net-4.0
google-qpx-express-api
noraui
form-data
atl
appfabric
swisscomdev
automake
starteam
graphenedb
optix
email-templates
pdf-reactor
angular-resource
parentheses
prolog-setof
rails-routing
typescript1.8
return-value
rdfs
lowpass-filter
directory-structure
memory-alignment
static-ip-address
elements
dstu2-fhir
measures
fuzzy-search
flash-cs5
testng-dataprovider
tform
maven-tomcat-plugin
wso2cloud
execute
msys
intel-fortran
purge
security-testing
embedded-code
deis
myo
sailfish-os
unity3d-gui
formatjs
c3
jscript.net
qpainter
sgen
xceed-datagrid
undefined-reference
inbox
dataservice
easy-install
sabredav
referrer
angularjs-controller
google-email-migration
venn-diagram
chrono
wse3.0
enter
nsmanagedobject
data-loss
hamachi
recent-documents
android-sdk-2.1
gallio
memory-size
gacutil
wsdl.exe
paul-graham

Resources

Encrypt Message