简体   繁体   中英

Calling a local variable in a class from another class

im having a trouble while i call acce variable of this class

public class Slider extends AppCompatActivity {

    ...

        public double[] acce={3.00,2.00,1.00};
        public double[] gyro={3.00,2.00,1.00};

            @Override
            protected void onCreate(Bundle savedInstanceState) {

            ...
            accelerometer = new Accelerometer(this);
        gyroscope = new Gyroscope(this);

        accelerometer.setListner(new Accelerometer.Listner() {
            @Override
            public double[] onTranslation(float ax, float ay, float az) {
                acce = setAccValue(ax,ay,az);
                return acce;
            }
...
}

from this class

public void AntremanSayfasi(Context context){
   
    ...
            Slider slider = new Slider();
            AntremanaBasla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aa = (float) slider.acce[0];
                bb = (float) slider.acce[1];
                cc = (float) slider.acce[2];
                accx.setText(String.valueOf(aa));
                accy.setText(String.valueOf(bb));
                accz.setText(String.valueOf(cc));
            }
        });
}

the problem is... i can get the determined values but cannot get the updated values of sensor change? sorry for rooky question, any help will be appritiated

UPDATE

this is the main Class

    public class Slider extends AppCompatActivity {

    private ViewPager AktifHareket;
    private LinearLayout mDotLayout;
    private String asd;
    private int asdf;
    private Antreman antreman;
    TextView DenemeSayiCek;
    int position;
    View view;
    int count;
    View viewFix;
    Context context;
//Sensors-------------------------------------------------------------------------------------------
    private Accelerometer accelerometer;
    private Gyroscope gyroscope;
    
    public double[] acc={3.00,2.00,1.00};
    public double[] gyr={3.00,2.00,1.00};
    public double[] acce={5.00,5.00,5.00};
    public double[] gyro={3.00,2.00,1.00};
    //Sensors---------------------------------------------------------------------------------------

    //Main------------------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Antreman antreman = new Antreman();
        context = this;
        setContentView(R.layout.activity_slider);
        Login login = new Login();
        AktifHareket = (ViewPager) findViewById(R.id.slideViewPager);
        mDotLayout = (LinearLayout) findViewById(R.id.dotsLayout);
        SliderAdapter sliderAdapter = new SliderAdapter(this);
        AktifHareket.setAdapter((sliderAdapter));

        addDotsIndicator();

        //Saydir Buton------------------------------------------------------------------------------
        TextView Deneme = (TextView) findViewById(R.id.deneme);
        TextView Deneme4 = (TextView) findViewById(R.id.deneme4);
        TextView Deneme3 = (TextView) findViewById(R.id.deneme3);
        TextView Deneme2 = (TextView) findViewById(R.id.deneme2);
        Button Saydir = (Button) findViewById(R.id.SaydirSlide);
        Button Saydir2 = (Button) findViewById(R.id.Saydir2Slide);
        Saydir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                position = AktifHareket.getCurrentItem();
                view = AktifHareket.getChildAt(position);
                count = Integer.valueOf(AktifHareket.indexOfChild(view));
                viewFix = AktifHareket.getChildAt(count);
                DenemeSayiCek = (TextView) viewFix.findViewById(R.id.TekrarSayisiSlide);
                asd = (String) DenemeSayiCek.getText().toString();
                asdf = Integer.valueOf(asd);
                asdf++;
                DenemeSayiCek.setText(String.valueOf(asdf));
                Deneme.setText(Arrays.toString(acce));
                Deneme2.setText(String.valueOf(position));
                Deneme3.setText(Arrays.toString(acce));
                Deneme4.setText(String.valueOf(SliderAdapter.POSITION_NONE));

            }
        });

        //Saydir Buton------------------------------------------------------------------------------
        //ImageButton-------------------------------------------------------------------------------
        ImageView logoImage =  (ImageView) findViewById(R.id.logo);
        logoImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antreman.AntremanSayfasi(context);
            }
        });
        antreman.AntremanSayfasi(context);//Açılışta Sayfayı açsın diye
        //ImageButton-------------------------------------------------------------------------------
        //Sensors-----------------------------------------------------------------------------------
        accelerometer = new Accelerometer(this);
        gyroscope = new Gyroscope(this);

        accelerometer.setListner(new Accelerometer.Listner() {
            @Override
            public double[] onTranslation(float ax, float ay, float az) {
                acce = setAccValue(ax,ay,az);
                Antreman.SensorValues.accValues=acce;
                return acce;
            }

        });

        gyroscope.setListner(new Gyroscope.Listner() {
            @Override
            public void onRotation(float gx, float gy, float gz) {
                setGyroValue(gx,gy,gz);
            }
        });

        //Sensors-----------------------------------------------------------------------------------
    }
    //Sensors---------------------------------------------------------------------------------------
    protected void onResume(){
        super.onResume();

        accelerometer.register();
        gyroscope.register();

    }
    protected void onPouse(){
        super.onPause();

        accelerometer.unregister();
        gyroscope.unregister();

    }

    public double[] setAccValue(double ac, double bc, double cc){
        this.acc[0] = ac;
        this.acc[1] = bc;
        this.acc[2] = cc;
        return acc;
    }

    public double[] setGyroValue(float qq, float wq, float eq){
        this.gyr[0] = qq;
        this.gyr[1] = wq;
        this.gyr[2] = eq;
        return gyr;
    }

    public void getAccValue(double ac, double bc, double cc){
        ac = (float) acce[0];
        bc = (float) acce[1];
        cc = (float) acce[2];

    }
    public void getGyroValue(float qq, float wq, float eq){

        qq = (float) gyro[0];
        wq = (float) gyro[1];
        eq = (float) gyro[2];

    }

    //Sensors---------------------------------------------------------------------------------------
    public void addDotsIndicator(){

        TextView[] mDots = new TextView[3];

        for(int i = 0; i < mDots.length; i++){
            mDots[i] = new TextView(this);
            mDots[i].setText(Html.fromHtml("&#8226;"));
            mDots[i].setTextSize(35);
            mDots[i].setTextColor(getResources().getColor(R.color.colorPrimaryDark));

            mDotLayout.addView(mDots[i]);
        }

    }

    //----------------------------------------------------------------------------------------------

}

ant this is the class i need to initialize those values from the main class

public class Antreman {
    private Accelerometer accelerometer;
    private Gyroscope gyroscope;
    public double aa;
    public float bb;
    public float cc;
    public float qq;
    public float ww;
    public float ee;

    //Pop up----------------------------------------------------------------------------------------
        public void AntremanSayfasi(Context context){
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        final View popup_durum = li.inflate(R.layout.popup_durum,null);
        Button AntremanaBasla = popup_durum.findViewById(R.id.basla);
        Button Cikis = popup_durum.findViewById(R.id.cikis);

        dialogBuilder.setView(popup_durum);
        AlertDialog dialog = dialogBuilder.create();
        dialog.show();

            TextView gyrox = (TextView) popup_durum.findViewById(R.id.gyrox);

        AntremanaBasla.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aa = SensorValues.accValues[0];
                gyrox.setText(String.valueOf(aa));

            }

        });
        Cikis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    static class SensorValues{
        public static double[] accValues = {1.00};
    }
}

you can use the concept of common class in this case and I believe it will solve your problem quite easily. Follow to steps below.

Make a public class and declare an array of double inside it which you need to access in another class

  Public class Common{
         public static List<double> acce;
    }

and in your activity where the sensors give updated value use a temp list and assign it to Common.acce;

Common.acce = tempList;

Now, when you need to access acce just use Common.acce , and the reference of tempList is in the Common.acce and when the data in tempList will be updated by your sensor the list in common will be also updated, so ultimately you will get the updated value.

Feel free to ask if something is unclear. And Kindly mark it as the correct answer if it helps you so that in future this answer can help other needy. Happy Coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM