java - why the array reinitialize each time? -
i've question you:
i wrote code:
public class mainactivity extends activity implements sensoreventlistener { private sensormanager msensormanager; private sensor msensor; arraylist<string> oldvalue = new arraylist<string>(); int count; @override public void oncreate(bundle savedstate) { super.oncreate(savedstate); setcontentview(r.layout.activity_main); msensormanager = (sensormanager) getsystemservice(context.sensor_service); msensor = msensormanager.getdefaultsensor(sensor.type_accelerometer); msensormanager.registerlistener((sensoreventlistener) this, msensor, sensormanager.sensor_delay_normal); count = 0; //oldvalue = null; } @override protected void onpause() { super.onpause(); //msensormanager.unregisterlistener(this); } public void onaccuracychanged(sensor sensor, int accuracy) { } public void onsensorchanged(sensorevent event) { string x,y,z; float xnum,ynum,znum; textview xtextview,ytextview,ztextview = new textview(this); if (oldvalue == null || oldvalue.size()<1) { string statoarray = "null or 0"; toast.maketext(this, statoarray, toast.length_short) .show(); } else { string statoarray = string.valueof(oldvalue.size()); toast.maketext(this, statoarray, toast.length_short) .show(); } if (oldvalue.size()<2) { xtextview=(textview)findviewbyid(r.id.x); ytextview=(textview)findviewbyid(r.id.y); ztextview=(textview)findviewbyid(r.id.z); xnum = event.values[0]; ynum = event.values[1]; znum = event.values[2]; oldvalue.add(string.valueof(xnum)); oldvalue.add(string.valueof(ynum)); oldvalue.add(string.valueof(znum)); x = float.tostring(math.round(xnum)); y = float.tostring(math.round(ynum));; z = float.tostring(math.round(znum));; xtextview.settext(x); ytextview.settext(y); ztextview.settext(z); } //toast.maketext(this, "pieno", toast.length_short) //.show(); } }
so don't understand because each time change value of x y z, array reinitialize itself...
i've necessity store more cicle in array value
thanks lot
because initializing them inside onsensorchanged()
so when device moves little, onsensorchanged()
called ,
string x,y,z; float xnum,ynum,znum; textview xtextview,ytextview,ztextview = new textview(this);
these , other things initialized onsensorchanged()
reinitialized
so declare these inside class
and use inside class
Comments
Post a Comment