Android - 从应用程序类更改活动 UI

3

我扩展了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();

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

或者传递一个上下文?有什么想法吗?

3个回答

4
我会创建一个监听器:
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。再次感谢! - Ofek Agmon
@OfekAgmon 感谢您的纠正!我已经修复了它!请告诉我它的运行情况。不用客气! - mmark

0
简短的回答是你不能直接从另一个活动引用UI。我的建议是在你的Application类上设置一个回调,在executeRequest#onPostExecute上调用它,然后在你的Activity上实现该回调并从那里更新你的UI。
如果你需要帮助来实现回调,请查看this question

0

如果您需要显示消息,Dialog类或Toast类是不错的选择,您可以在这里查看更多信息: 对话框:http://developer.android.com/guide/topics/ui/dialogs.html Toast:http://developer.android.com/guide/topics/ui/notifiers/toasts.html

但是,如果您想要访问或修改实际活动中的控件,则使用Runnable类,并使用context.runOnUiThread()方法(如果您在AsyncTask内工作)。真正的问题是您无法使用控件的声明在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");
                }
}

如果我能帮到你,请告诉我,祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接