Search code examples
javaandroidandroid-viewgetview

Access View from listener or custom function


Hello

I'm learning how GPS works in Android (And generally android :) ), and I'm trying to access my View from a listener and I can't, I need to update my TextView in "onGpsStatusChanged" and I don't have my View there, So, For example, in this project: https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java I want to access the View in the function:

public void onGpsStatusChanged(int event, GpsStatus status) { 
...
updateStatus(status,v);
...
}

private void updateStatus(GpsStatus status) {
//Use the View Here:
**mLatitudeView = (TextView) v.findViewById(R.id.latitude);**
}

but I can't since "v" is not defined in the function "onGpsStatusChanged"..

How can I access my view here? I think I should Use an Adapter with GetView, But Im not sure how..

Here is My Code:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mRes = getResources();
        View v = inflater.inflate(R.layout.gps_status, container,
                false);

        mLatitudeView = (TextView) v.findViewById(R.id.latitude);
        mLongitudeView = (TextView) v.findViewById(R.id.longitude);
        mAccuracyView = (TextView) v.findViewById(R.id.accuracy);
        mSpeedView = (TextView) v.findViewById(R.id.speed);
        mSatsView = (TextView) v.findViewById(R.id.satsl);
        mStrengthView = (TextView) v.findViewById(R.id.satst);

        mLatitudeView.setText(EMPTY_LAT_LONG);
        mLongitudeView.setText(EMPTY_LAT_LONG);


        GpsTestActivity.getInstance().addListener(this);

        return v;
    }

public void onGpsStatusChanged(int event, GpsStatus status) {

        switch (event) {
            case GpsStatus.GPS_EVENT_STARTED:
                setStarted(true);
                break;

            case GpsStatus.GPS_EVENT_STOPPED:
                setStarted(false);
                break;

            case GpsStatus.GPS_EVENT_FIRST_FIX:
                break;

            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                updateStatus(status);
                break;
        }
    }

private void updateStatus(GpsStatus status) {

        setStarted(true);
        Iterator<GpsSatellite> satellites = status.getSatellites().iterator();

        if (mPrns == null) {
            int length = status.getMaxSatellites();
            mPrns = new int[length];
            mSnrs = new float[length];
            mSvElevations = new float[length];
            mSvAzimuths = new float[length];
        }

        mSvCount = 0;
        mStrenght = 0;
        mAlmanacMask = 0;
        mUsedInFixMask = 0;
        while (satellites.hasNext()) {
            satellites.next();

            mSvCount++;
        }
        mSatsView.setText(mSvCount + " satellites");

        if(mAcc>100 || mAcc == 0)
        {
            fPercentage = 0;
        }else{

            fPercentage = (int)(Math.round((100-mAcc)/2));
        }

        if(mSvCount<11){

            mStrenght = mSvCount*5;
        }else{
            mStrenght = 50;
        }



        mStrengthView.setText((mStrenght + fPercentage) + " %");


       // final ArcProgress arcProgress = (ArcProgress) v.findViewById(R.id.arc_progress);
//I wand to Update the Progress Here and I cant:
        //arcProgress.setProgress(mStrenght+fPercentage);

    }

Thank you!


Solution

  • Thanks for @ssuukk, The answer is: getActivity().... or in other words:

    final ArcProgress arcProgress = (ArcProgress) getActivity().findViewById(R.id.arc_progress);
            arcProgress.setProgress(mStrenght+fPercentage);
    

    Thank you all for your help!