【问题标题】:Android update ui elements from other classAndroid 更新其他类的 ui 元素
【发布时间】:2017-01-17 19:12:57
【问题描述】:

我有一个关于 android 编程的问题。我有一个 MainActivity 类和实现 LocationListner 的类。我想从第二个类的 onLocationChanged 方法中更改一些 UI 元素,但我不知道应该如何完成这样的事情。

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

private LocationGPS gps;
private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    enableGPS();
    initGuiElements();
}

private void enableGPS()
{
    gps = new LocationGPS(this);
}

private void initGuiElements()
{
    text = (TextView) findViewById(R.id.textTop);
    text.setText("nothing yet");
}

}

这是我的位置类。

public class LocationGPS implements LocationListener {

private Activity mainActivity;
private LocationManager locationManager;

public LocationGPS(Activity activity)
{
    this.mainActivity = activity;
    locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) {
    //here I want to set text for TextView element
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

【问题讨论】:

  • 在你的activity中调用一个方法或者传递你的rootView

标签: android user-interface


【解决方案1】:

你有很多方法可以做到这一点,我会给出其中的 2 个:

  1. 更改您的活动以实现 LocationListener 并且当您调用 locationManager.requestLocationUpdates 时将活动作为侦听器传递,这样您将获得在 Activity 而不是在 LocationGPS 类中调用的回调,MainActivity 将如下所示:

    public class MainActivity extends AppCompatActivity
        implements LocationListener {
    
    private LocationGPS gps;
    
    private TextView text;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        enableGPS();
        initGuiElements();
    }
    
    private void enableGPS() {
        gps = new LocationGPS(this);
    }
    
    private void initGuiElements() {
        text = (TextView) findViewById(R.id.textTop);
        text.setText("nothing yet");
    }
    
    @Override
    public void onLocationChanged(Location location) {
    
    }
    
    @Override
    public void onStatusChanged(String provider, int status,          
    Bundle extras) {
    
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    
    }
    }
    

    还有 LocationGPS 类:

    public class LocationGPS {
    
    private Activity mainActivity;
    
    private LocationManager locationManager;
    
    public LocationGPS(Activity activity) {
        this.mainActivity = activity;
        locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mainActivity);
    }
    }
    
  2. 第二种方法是你可以在 MainActivty 中创建一个通信接口,你需要将它传递给 LocationGPS 类,一旦你想更新 Activity 中的 UI,你可以用下一种方法调用这个接口方法:

    public class MainActivity extends AppCompatActivity implements
        MainActivity.MainActivityInteractionInterface{
    
    private LocationGPS gps;
    
    private TextView text;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        enableGPS();
        initGuiElements();
    }
    
    private void enableGPS() {
        gps = new LocationGPS(this, getApplicationContext());
    }
    
    private void initGuiElements() {
        text = (TextView) findViewById(R.id.textTop);
        text.setText("nothing yet");
    }
    
    @Override
    public void updateUI() {
       text.setText("Some new text");
    }
    
    public interface MainActivityInteractionInterface {
        void updateUI();
    }
    }
    

LocationGPS 现在看起来像:

    public class LocationGPS implements LocationListener{

    private MainActivity.MainActivityInteractionInterface interactionInterface;

    private LocationManager locationManager;

    private Context applicationContext;

    public LocationGPS(MainActivity.MainActivityInteractionInterface interactionInterface, Context applicationContext) {
        this.interactionInterface = interactionInterface;
        this.applicationContext = applicationContext;
        locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        interactionInterface.updateUI(); // Or call this method whenever you need 
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    }

【讨论】:

  • 我喜欢你的第二个想法,它看起来更好。但是现在在 LocationGPS ctor 中我没有活动,所以如何调用 getSystemService?
  • @boringBob 请查看我的更新答案。只是为了记录,在防止内存泄漏方面,传递应用程序上下文而不是活动上下文总是更好,因为它具有单个实例。如果此答案符合您的要求,请将其标记为已接受。
【解决方案2】:

我发现 Artyom Okun 的第二个代码建议很有帮助,但它确实需要一些调试。我不得不将界面移动到它自己的文件中。这是由于“循环继承”导致您尝试继承要在 Activity 中创建的对象。

See this stackoverflow page explaining the issue

MainActivity.java

public class MainActivity extends AppCompatActivity implements
    MainActivityInteractionInterface{

private LocationGPS gps;

private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    enableGPS();
    initGuiElements();
}

private void enableGPS() {
    gps = new LocationGPS(this, getApplicationContext());
}

private void initGuiElements() {
    text = (TextView) findViewById(R.id.textTop);
    text.setText("nothing yet");
}

@Override
public void updateUI() {
   text.setText("Some new text");
}


}

位置GPS.java

public class LocationGPS implements LocationListener{

    private MainActivityInteractionInterface interactionInterface;

    private LocationManager locationManager;

    private Context applicationContext;

    public LocationGPS(MainActivityInteractionInterface interactionInterface, Context applicationContext) {
        this.interactionInterface = interactionInterface;
        this.applicationContext = applicationContext;
        locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        interactionInterface.updateUI(); // Or call this method whenever you need 
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    }

然后新建文件MainActivityInteractionInterface.java

public interface MainActivityInteractionInterface {
    void updateUI();
}

为了更新 UI,我在 LocationGPS 类的 onLocationChanged() 方法中更新了一个全局变量。然后在 updateUI() 我使用全局变量写入文本视图。

注意在 MainActivity 中写入全局,如下所示:

MainActivity.bestLat = 纬度;

【讨论】:

    【解决方案3】:

    在你的 MainActivity 中创建一个方法

    public void updateText(String updatedText){
     text.setText(updatedText);
    }
    

    在你的 LocationGPS 类中,你已经有了 MainActivity 的上下文,所以在 onLocationChanged 中,调用这个方法

    mainActivity.updateText(updatedText);
    

    还有另一种个人推荐的方式。创建一个接口并在 onLocationChanged 方法中调用它。在您的 MainActivity 中实现并在已实现的界面中更新您的文本。 我更喜欢这种方法,因为您不需要共享 Activity 的上下文或 rootview,这可以防止可能的内存泄漏。

    【讨论】:

      【解决方案4】:

      只需将 LocationGps 类放在 MainActivity 类中。完毕。 抱歉无法发表评论,因此发布为答案。

      【讨论】:

        【解决方案5】:

        这对你有帮助

        public class LocationGPS implements LocationListener {
        
        private Activity mainActivity;
        private LocationManager locationManager;
        
        public interface OnChangeListener{
         public void onChangeListener();
        
        }
        
        OnChangeListener mOnChangeListener;
        
        public LocationGPS(Activity activity,OnChangeListener onChangeListener)
        {
            this.mainActivity = activity;
          mOnChangeListener = onChangeListener;
          locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
        
        @Override
        public void onLocationChanged(Location location) {
        
        //here I want to set text for TextView element
         mOnChangeListener.onChangeListener();   
        
        }
        
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        
        }
        
        @Override
        public void onProviderEnabled(String provider) {
        
        }
        
        @Override
        public void onProviderDisabled(String provider) {
        
        }
        

        Activity 必须实现接口 OnChangeListener

        public class MainActivity extends AppCompatActivity implements OnChangeListener {
        
        private LocationGPS gps;
        private TextView text;
        
        @Override
         public void onChangeListener(){
        //For example you may edit TextView
        text.setText("data recieved:onLocationChanged")
        }
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        
            enableGPS();
            initGuiElements();
        }
        
        private void enableGPS()
        {
        
        //You transfer interface
          gps = new LocationGPS(this,this);
        }
        
        private void initGuiElements()
        {
            text = (TextView) findViewById(R.id.textTop);
            text.setText("nothing yet");
        }
        

        【讨论】:

          猜你喜欢
          • 2015-06-10
          • 1970-01-01
          • 1970-01-01
          • 2018-11-06
          • 1970-01-01
          • 1970-01-01
          • 2012-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多