【问题标题】:android - changing Activity UI from application classandroid - 从应用程序类更改 Activity UI
【发布时间】:2015-05-25 15:25:24
【问题描述】:

我扩展了 Application 类以便在 android 中创建类似单例的对象。

在这个对象中,我的服务器拥有所有 HTTP 工作,所有其他活动都可以访问它并调用 GET、POST 等方法。

代码:

public class HttpManagerInstance extends Application {
    private HttpClient httpClient;
    private HttpGet get;

    @Override
    public void onCreate() {
        httpClient = new DefaultHttpClient();
        get = new HttpGet("http://10.100.102.9:8000/users/");
        super.onCreate();

    }


    public Void getUsers() throws Exception {
        new executeRequest().execute(get);
        return null;
    }

    private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {

        @Override
        protected Integer doInBackground(HttpRequest... params) {
            // TODO Auto-generated method stub
            HttpRequest request = params[0];
            HttpResponse response;
            String result="";
            try {
                response = httpClient.execute((HttpUriRequest) request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return responseCode;
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            switch (result) {
            case HttpStatus.SC_OK:
                // request was fine

                // Here I want to updated the GUI of the activity that called this method.
                break;
            }
        }

    }

}

这就是我从 Activity 调用方法的方式:

HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
sampleApp.getUsers();

再次 - 我想访问调用该方法的 Activity 的 UI 以放置 REQUEST ACCEPTED 消息。

也许传递一个上下文?有什么想法吗?

【问题讨论】:

    标签: android user-interface android-activity android-context


    【解决方案1】:

    我会创建一个监听器:

    public class HttpManagerInstance extends Application {
        private HttpClient httpClient;
        private HttpGet get;
    
        public interface ResponseListener{
          public void onSuccess(Object data);
        }
    
    
        @Override
        public void onCreate() {
            httpClient = new DefaultHttpClient();
            get = new HttpGet("http://10.100.102.9:8000/users/");
            super.onCreate();
    
        }
    
    
        public Void getUsers(ResponseListener listener) throws Exception {
            new executeRequest(listener).execute(get);
            return null;
        }
    
        private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {
    
            private ResponseListener mListener;
    
            public executeRequest(ResponseListener listener){
             this.mListener = listener;
            }
    
            @Override
            protected Integer doInBackground(HttpRequest... params) {
                // TODO Auto-generated method stub
                HttpRequest request = params[0];
                HttpResponse response;
                String result="";
                try {
                    response = httpClient.execute((HttpUriRequest) request);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return responseCode;
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                // TODO Auto-generated method stub
                switch (result) {
                case HttpStatus.SC_OK:
                    // request was fine
    
                    // Here I want to updated the GUI of the activity that called this method.
                    if(this.mListener != null) mListener.onSuccess(whatEverDataYouWant);
                    break;
                }
            }
    
        }
    
    }
    

    然后,在你的活动中:

        HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
        sampleApp.getUsers(new ResponseListener(){
           public void onSuccess(Object data){
             //update your ui!
           }
    
    });
    

    【讨论】:

    • 感谢您的回答。看起来真的很好,我会试试的。顺便说一句 - 我认为你有一个小错误 - 在 Activity 代码中有 ResponseListener 是接口,在 AsyncTask 你写了 RequestListener 不存在。再次感谢!
    • @OfekAgmon 感谢您的更正!我刚刚修好了!让我知道进展如何。不客气!
    【解决方案2】:

    简短的回答是您不能直接从另一个活动中引用 UI。我的建议是让你在 Application 类上设置回调并在 executeRequest#onPostExecute 上调用,然后在 Activity 上实现该回调并从那里更新你的 UI。

    如需帮助实现回调检查this question

    【讨论】:

      【解决方案3】:

      如果您需要显示消息是 Dialog Class 或 Toast Class 的好选择,您可以在此处查看更多信息: 对话:http://developer.android.com/guide/topics/ui/dialogs.html 祝酒词:http://developer.android.com/guide/topics/ui/notifiers/toasts.html

      但是,如果您想在实际活动中访问或修改控件,则使用 Runnable 类,如果您在 AsyncTask 中工作,则使用 context.runOnUiThread() 方法。真正的问题是您无法使用控件声明更改 AsyncTask 中的 UI。你需要抛出一个 Runnable 进程来与活动通信!!。例如:

      context.runOnUiThread(new Runnable() {
                      public void run() {
                          //Declaration of variables
                          TextView MyTextView = (TextView) context.findViewById(R.id.txtvMyControl);
      
                          MyTextView.setText("My title");
                      }
      }
      

      如果我能帮你说我,祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-28
        • 2015-10-28
        • 2017-12-06
        • 2016-11-16
        • 2021-12-16
        • 1970-01-01
        相关资源
        最近更新 更多