有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android从第二个类调用类函数

如何从类#B调用类#a中定义的函数?类#B扩展了AsynchTask并从web服务获取远程数据。类#我试图从类#B调用的函数用于发送检索到的远程数据,以供类#A显示

我试图使用this将类#A的当前实例传递给类#B,但这只是传递上下文,因此无法识别函数

我也尝试过使用static,但是当函数运行一个新线程时,将函数定义为static会产生一个编译器错误

我试图调用的代码如下:

public void test(List<DATA> Data){
        this.Data = Data;
        runOnUiThread(new Runnable() {          

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for(DATA data : MainActivity.this.Data){
                    Toast.makeText(MainActivity.this, data.title, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

共 (3) 个答案

  1. # 1 楼答案

    From your question, I understood that u want to show the result of asynctask in another class. You can use onProressUpdate api of asynctask. You can do the work in doInBackground and then call publishProgress(value) whihc in turn calls onProgressUpdate and run on UI thred.
    
     private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
         protected Long doInBackground(URL... urls) {
             int count = urls.length;
             long totalSize = 0;
             for (int i = 0; i < count; i++) {
                 totalSize += Downloader.downloadFile(urls[i]);
                 publishProgress((int) ((i / (float) count) * 100));
                 // Escape early if cancel() is called
                 if (isCancelled()) break;
             }
             return totalSize;
         }
    
         protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }
    

    其他方式:

    您可以在类#a中定义一个处理程序,并从类#B中调用它

    Sorry if i mis understood your question.
    
  2. # 2 楼答案

    您可以制作一个界面,即:

    public interface RecieveDataDelegate {
    
        public void receiveData(Object dataFromWebService, boolean success);
    }
    

    让A类实现接口。从类#调用asyncTask时,将自身传递给asyncTask的构造函数,该构造函数具有ReceiveDataDelegate类型的参数

    new AsyncTask1 (this).execute();
    

    使用构造函数设置AsyncTask,并将数据发送回类#a onPostExecute(),以便更新ui

     class AsyncTask1 extends AsyncTask<Void, Void, Boolean> {
        Object dataReceived ;
        protected RecieveDataDelegate delegate;
    
        public AsyncTask1 (RecieveDataDelegate delegate) {
            this.delegate = delegate;
        }
        protected Boolean doInBackground(Void... params) {
          //do your service request and set dataRecieved
        }
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            delegate.RecieveDataDelegate (dataReceived, result.booleanValue());
        }
    

    这样,任何实现该接口的类都可以使用相同的asyncTask

  3. # 3 楼答案

    据我所知,你从一个异步任务B开始,因为在Android中,web抓取只能以异步方式工作。您希望对中的对象可以访问获取的结果

    A会编写这样的代码

    public class A extends FragmentActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            TextView fetch = (TextView) findViewById(R.id.fetch);
            new B(getApplicationContext(),fetch).execute();
            // This will update a TextView when fetching is done.
        }
    }
    
    public class B extends AsyncTask<Void, Integer, Void> {
        Context c;
        TextView f;
        public B(Context c, TextView f) {
            super();
            this.f = f;
            this.c = c;
        }
    
    
        @Override
        protected Void doInBackground(Void... params) {
            // TODO: the fetching: fetch()
            String t = fetch();
            f.setText();
            return null;
        }
    }
    

    您可以使用执行前和执行后方法执行其他技巧。我希望我能帮忙