访问活动UI元素,从另一个类 [英] accessing UI elements of activities from another class

查看:192
本文介绍了访问活动UI元素,从另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧首先的Andr​​oid真的是混乱。该方案是我有一个是从登录查看创建约两可运行类,如果登录会造成另一种观点认为,这将有其他数据,并可以从那里创造更加活动

Ok first of all android is really confusing. The scenario is I have about two runnable classes which are created from a Login View and if logged in it will create another view which will have other data and even more activities can be created from there

现在我可以创建可运行的类线程时通过登录查看内容和编辑像这样在他们的UI元素:

Now I can pass the the Login view context when creating a thread for the runnable class and edit out UI elements in them like this:

((Activity)someContext).runOnUiThread(new Runnable(){
    public void run()
    {
        TextView txtErr = (TextView) ((Activity)someContext).findViewById(R.id.errMsg);
        txtErr.setText("Some message");
    } 
});

但问题是会出现的将被创建并在登录的时候创建运行的类更多的活动,我不能让路过的上下文。

But the issue is there will be more activities that will be created and the runnable class is created at the time of logging in, and I can't keep passing contexts.

是否有从不同的线程访问不同的活动的UI元素更好的办法?

Is there a better way for accessing the UI elements of different activities from different threads?

P.S:将要访问的UI元素的线程不扩展活动,并在一个单独的线程运行

P.S: the threads which will be accessing the UI elements doesn't extend Activity and are running in a separate thread.

修改

我想我需要让我的问题更清楚......我开发一个信使客户端应用程序...的过程进行这样...上登录按钮,用户点击它创建一个单独的类线程命名 ClientThread 处理套接字连接并保持连接活着,直到用户注销或连接断开。在 ClientThread 类循环,直到套接字连接,而且每当收到了一些数据将数据传递给一个名为类另一个线程 ProcessDataThread 里面做数据分析,并相应更新UI。

I think I need to make my question more clear... I am developing a client app for a messenger... The process goes this way... User clicks on login button which creates a thread in a separate class named ClientThread for handling socket connection and keeping the connection alive till the user logs out or connection drops. The ClientThread class loops till the socket is connected and whenever some data is received the data is passed to another thread in a class named ProcessDataThread which do the parsing of data and will update the UI accordingly.

现在,从服务器的响应,如果用户登录我要创建该类的活动,并保持上下文中 ProcessDataThread 这项活动,我会从服务器的进一步答复更新UI。如果登录失败 ProcessDataThread 将在主要活动显示一条消息,登录失败,现在我能够通过传递上下文实现以后 MainActivity 来两个线程时登录点击是这样的:

Now in a response from server if the user is logged in I want to create an activity from that class and keep a context to that activity in ProcessDataThread as I will be updating UI on further responses from server. And if login fails ProcessDataThread will display a message on the main activity saying login failed, now I was able to achieve the later by passing the context from the MainActivity to the two threads when clicked on Login like this:

global_constants.clientObject = new ClientThread(this);
global_constants.clientThread = new Thread(global_constants.clientObject);
global_constants.clientThread.start();

再从 ClientThread ProcessDataThread

global_constants.updateConversationHandler.post(new ProcessDataThread(SharedBuff, cntxt));

但我怎么会从一个非活动类创造更多的活动,并尽一切更新他们或找一个UI元素等等......

But how will I create more activities from a non-activity class and do all update them or find a UI element etc...

推荐答案

不知道如果我理解你,但它听起来就像你正试图控制从活动以外的活动的看法。这听起来很哈克给我。我让每个活动管理​​自己的UI。

Not sure if I understand you, but it sounds like you are trying to control the view of an activity from outside of the Activity. This sounds hacky to me. I'd let each Activity manage its own UI.

做对象之间的解耦通信的一个好方法是观察花纹,又名事件总线或事件调度制度。如何做到这一点在Android上的一个例子是在这里:的http:// www.therealjoshua.com/2012/03/event-dispatching-sending-messages/

A good way of doing decoupled communication between objects is the observer pattern, aka an "event bus" or "event dispatcher" system. An example of how to do this on Android is here: http://www.therealjoshua.com/2012/03/event-dispatching-sending-messages/

基本上,code,它的产生误差应派遣的消息。在活动可以听此消息,然后根据需要更新自己的UI。

Basically, the code that's generating the error should dispatch a message. The Activity can listen for this message, and then update its own UI as needed.

修改

感谢您的澄清。我认为观察者模式仍然可以帮助在这里。基本上,你的数据处理线程应该知道关于UI什么。只要有他们发布一个事件有关的错误,可选地在错误的附加信息。如果你想,你的事件调度类可以使用甚至使UI线程本身的实际事件调用的的Runnable 像你这样的表现,让听者总是可以假定它们是被称为在UI线程上,如果这是你的设计很重要。这样你就不必上下文传递给线程在所有(至少不是用于更新UI的目的) - 让工作线程正好负责的工作,并在活动可以负责它自己的UI。

Thanks for the clarification. I think the observer pattern can still help here. Basically, your data processing threads shouldn't know anything about the UI. Just have them post an event for the error, optionally with additional info on the error. If you want, your event dispatcher class could even make the actual event calls on the UI thread itself using a Runnable like you showed, so that the listener can always assume that they are being called on the UI thread, if this is important for your design. This way you don't have to pass the context to the thread at all (at least not for purposes of updating the UI) - let the worker thread just be responsible for the work, and the activity can be responsible for its own UI.

您可以使用另一种选择是一个Android 处理程序(见的 http://developer.android.com/reference/android/os/Handler.html
在这种情况下,工作在另一个线程仍在进行,但活动从线程在收到的handleMessage 回调适当的时间。我没有用这个,但是我从文档,它看起来像它可以把工作给你需要的东西做的。

Another option you could use is an android Handler (see http://developer.android.com/reference/android/os/Handler.html) In this case, the work is still done in another thread, but the Activity receives a handleMessage callback from the thread at the appropriate time. I haven't used this myself but from the documentation it looks like it can get the job done for what you need.

在这两种情况下IMO,用于更新UI应该出在活动的责任,而不是工作线程。

In either case IMO, the responsibility for updating the UI should lie with the Activity, not the worker thread.

这篇关于访问活动UI元素,从另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆