从侦听器或自定义函数访问视图
Access View from listener or custom function
你好
我正在学习 GPS 在 Android 中的工作原理(通常是 android :)),我正在尝试从侦听器访问我的视图,但我不能,我需要在 "onGpsStatusChanged" 中更新我的 TextView 并且我在那里没有我的视图,因此,例如,在这个项目中:
https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java
我想访问函数中的视图:
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);
}
谢谢!
尝试使用:
public void onGpsStatusChanged(int event, GpsStatus status) {
...
View view = (View) findViewById(R.id.view);
updateStatus(status, view);
..
}
public void updateStatus(int status, View view) {
...
}
试试这个..
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");
}
感谢@ssuukk,答案是:getActivity()....
或者换句话说:
final ArcProgress arcProgress = (ArcProgress) getActivity().findViewById(R.id.arc_progress);
arcProgress.setProgress(mStrenght+fPercentage);
谢谢大家的帮助!
你好
我正在学习 GPS 在 Android 中的工作原理(通常是 android :)),我正在尝试从侦听器访问我的视图,但我不能,我需要在 "onGpsStatusChanged" 中更新我的 TextView 并且我在那里没有我的视图,因此,例如,在这个项目中: https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java 我想访问函数中的视图:
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);
}
谢谢!
尝试使用:
public void onGpsStatusChanged(int event, GpsStatus status) {
...
View view = (View) findViewById(R.id.view);
updateStatus(status, view);
..
}
public void updateStatus(int status, View view) {
...
}
试试这个..
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");
}
感谢@ssuukk,答案是:getActivity().... 或者换句话说:
final ArcProgress arcProgress = (ArcProgress) getActivity().findViewById(R.id.arc_progress);
arcProgress.setProgress(mStrenght+fPercentage);
谢谢大家的帮助!