从侦听器或自定义函数

时间:2015-09-15 09:14:18

标签: java android android-view getview

你好

我正在学习GPS在Android中的工作方式(通常是android :)),而且我试图从听众那里访问我的视图而且我不能,我需要更新我的TextView in" onGpsStatusChanged"并且我没有我的视图,所以,例如,在这个项目中: https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java 我想在函数中访问View:

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);**
}

但我不能" v"未在函数" onGpsStatusChanged" ..

中定义

如何在此处访问我的观点?我想我应该使用GetView的适配器,但我不知道如何..

这是我的代码:

 @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);

    }

谢谢!

3 个答案:

答案 0 :(得分:0)

尝试使用:

public void onGpsStatusChanged(int event, GpsStatus status) {
  ...
  View view = (View) findViewById(R.id.view);
  updateStatus(status, view);
  ..
}

public void updateStatus(int status, View view) {
  ...
}

答案 1 :(得分:0)

试试这个..

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

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

答案 2 :(得分:0)

感谢@ssuukk,答案是:getActivity().... 或换句话说:

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

谢谢大家的帮助!