How do I manipulate a View from another class in Android?

微笑、不失礼 提交于 2019-12-11 01:37:45

问题


I have a handler for receiving SMS in a seperate class. How can I make it change the view elements when the handler is executed?

Here is my code:

public class MainActivity extends MapActivity {
    MapView mapView;
    MapController mc;
    GeoPoint p;
    private int carNumber;
    private String langt;
    private String longt;
    CheckedTextView text;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SmsReceiver smsreceiver = new SmsReceiver();
                draw();
    }

    public void draw() {
        mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
        @SuppressWarnings("deprecation")
        View zoomView = mapView.getZoomControls();

        zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);
        mc = mapView.getController();

        double lat = Double.parseDouble("31.977185185185185");
        double lng = Double.parseDouble("35.205925925925925");

        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17);
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();
    }
}
}

Here is the SMS Receiver class:

public class SmsReceiver extends BroadcastReceiver {
    MapView mapView;
    MapController mc;
    GeoPoint p;
    private int carNumber;
    private String langt;
    private String longt;
    CheckedTextView text;

    public SmsReceiver(MapView mapView) {
        this.mapView = mapView;
    }
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @SuppressLint("NewApi") @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
            final SmsMessage smsMessage = extractSmsMessage(intent);
            processMessage(context, smsMessage);
        }
    }

    @SuppressLint("NewApi") private SmsMessage extractSmsMessage(final Intent intent) {
        final Bundle pudsBundle = intent.getExtras();
        final Object[] pdus = (Object[]) pudsBundle.get("pdus");
        final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);

        return smsMessage;
     }
}

I want to modify the MapView inside the SmsReceiver class.

Thank you.


回答1:


Define an interface and use a callback to let the activity know that an SMS has been received.

public Interface SmsReceivedListener {
    void onSmsReceived(int arg1, string arg2); ..<----add arguments you want to pass back
}

In your SMS class

ArrayList<SmsReceivedistener> listeners = new ArrayList<SmsReceivedistener>();

...

public void setSmsReceivedistener(SmsReceivedistenerlistener){
    listeners.add(listener);
}

When you receive an SMS

for (SmsReceivedistener listener:listeners){
   listener.onSmsReceived(arg1, arg2);
}

In your Activity:

public class MainActivity extends MapActivity implements SmsReceivedListener {

...

@Override
public void onCreate(Bundle savedInstanceState) 
{
    ...
    smsReceiver.setSmsReceivedistener(this);
    ...
}

public void onSmsReceived(int arg1, string arg2){
   // do whatever you need to do
}

All from memory so please excuse typos and you should improve the SMS class by adding removeSmsReceivedistener and checking that you do not add the same listener twice in setSmsReceivedistener.

Note. Because you use an interface, any class (not just an Activity) can implement it so you can update anywhere in your app. The smsReceiver class doesn't know or care. It just calls the listeners, if any are registered.




回答2:


Try this

TextView bt1 =m_activity.findViewById(R.id.textview1);

In the above example where view bt1 is from another class.you can access bt1 by using class instance and get the id.Now,do what ever you want do with bt1 instance.

Note: Make Sure bt1 is public in another class




回答3:


Add this to your onReceive() method:

if (context instanceof MainActivity) {
    //modify the map 
    ((MainActivity)context).findViewById(...);
}


来源:https://stackoverflow.com/questions/14388498/how-do-i-manipulate-a-view-from-another-class-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!