Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从外部类更新列表和适配器_Android - Fatal编程技术网

Android 从外部类更新列表和适配器

Android 从外部类更新列表和适配器,android,Android,我正在构建聊天应用程序。在Oncreate中,我使用以下方法连接到我的服务器: public void connectToJabber(String username,String password) { Intent jService=new Intent(this,ConnectionIntent.class); jService.putExtra("type", "login"); jService.putExtra("username",username);

我正在构建聊天应用程序。在
Oncreate
中,我使用以下方法连接到我的服务器:

public void connectToJabber(String username,String password) {
    Intent jService=new Intent(this,ConnectionIntent.class);
    jService.putExtra("type", "login");
    jService.putExtra("username",username);
    jService.putExtra("password",password);
    startService(jService);
}
    public class ListenMessages implements MessageListener {  
        @Override
        public void processMessage(Chat arg0,
                org.jivesoftware.smack.packet.Message arg1) {
            System.out.println(String.format("Received message:"+arg1.getBody()));  

        }  

    } 
你看,这是一项服务。这是我的
connectioncontent
服务:

    protected void onHandleIntent(Intent intent) {
        final Connection connection=((Trackers)getApplication()).getConnectionInstance();
        String process=intent.getExtras().getString("type");

        if (process.equals("login") == true) {
            //Login İşlemleri
            String username=intent.getExtras().getString("username");
            String password=intent.getExtras().getString("password");
            try {
                connection.login(username,password);
            } catch (XMPPException | SmackException | IOException e) {
                e.printStackTrace();
            }
        }
...
您可以看到,我正在使用
connection
变量进行连接。我从
connection
类中获取此变量

连接
类中,我有一个消息侦听器类。当我接收到消息时,它将进入此方法:

public void connectToJabber(String username,String password) {
    Intent jService=new Intent(this,ConnectionIntent.class);
    jService.putExtra("type", "login");
    jService.putExtra("username",username);
    jService.putExtra("password",password);
    startService(jService);
}
    public class ListenMessages implements MessageListener {  
        @Override
        public void processMessage(Chat arg0,
                org.jivesoftware.smack.packet.Message arg1) {
            System.out.println(String.format("Received message:"+arg1.getBody()));  

        }  

    } 
我的问题从这里开始。当我收到消息时,我必须将发件人的姓名添加到聊天列表中,并且我应该更新适配器。但是我在主活动中定义了聊天列表和适配器。因此我无法从
processMessage
访问它


我应该怎么做才能做到这一点呢?

所以,您需要的是一种在服务和活动之间进行沟通的方式

大多数情况下,我使用广播来解决这个问题(注意,有些变体是应用程序的本地变体),但也有绑定服务的方法等

编辑


我无法向您提供完整的示例,但您可以使用在活动中注册BroadcastReceiver(可以是异常类实例或内部类实例),并从服务中广播意图。这些缩进将由广播接收器接收。您可以通过extras将任何类型的数据放入意向书中,并将其放入广播接收器以供进一步使用。

您能举个例子吗?当消息到达时,我必须更新列表和适配器。请解释更多,我正在等待您的评论。您确定可以使用
catch(XMPPException | smakexception | IOException e)
?这不是Java1.7的一个特性吗?Android的Java只有1.6版本。它能工作吗?没有得到任何错误。