1

这是,我认为这是一个基本的java问题:

这是一个Android项目。我有这个设置:

 public class MyFragmentActivity extends FragmentActivity implements
    ActionBar.TabListener {

     // lots of code edited out


    public static class RateFragment extends Fragment {

             // lots of code edited out

         class InsertTask extends AsyncTask<String, String, Void> {

               protected void onPostExecute(Void v) { {

                // I need to access ReviewTask here
                new ReviewTask().execute();

               } 

         }


    }

    public static class ReviewFragment extendsListFragment {

              class ReviewTask extends AsyncTask<String, String, Void> {

                  // code

              }


    }


}

我真的在问一个基本的 Java 问题。我知道它不是 Android 中最好的方法。

如果您必须知道我在做什么:(如果这令人困惑,则此信息的优先级较低,仅针对某些上下文)当在 RateFragment 中按下按钮并将数据插入 MySQL 数据库时,我会调用 InsertTask。这就是我想在上面的代码中做的:在 InsertTask 中,最后,在另一个内部类中调用 ReviewTask。这将更新列表视图(位于另一个选项卡中)。

可以从 InsertTask 访问 ReviewTask 吗?

4

2 回答 2

4

I don't think you can do it. I just tried it out. Here's what you can do though... have a method inside the fragment which the InsertTask's postExecute() method calls on. In this fragment method, you can create a new ReviewTask and fire the task. This way your inner classes just talks to your parent class which acts as a delegate between calls.

EDIT:

public class MyFragment extends FragmentActivity {

        private void delegateMethod(Object result) {
            TaskB b = new TaskB();
            b.doanotherthing(result);
        }

        class TaskA {
            ...
            public void onPostExecute() {
                Object result = new Object();
                delegateMethod(result);
            }
        }

        class TaskB {
            public void doanotherthing(Object o) {
                ...
            }
        }
    }
于 2012-07-26T02:49:03.247 回答
1

如果您将 RateFragment 的声明更改为非静态,您可以调用

class InsertTask extends AsyncTask<String, String, Void> {

    protected void onPostExecute(Void v) { {

        MyFragmentActivity.this.(new RateFragment()).(new ReviewTask()).execute();

    } 

}

Because your InsertTask is still inside an instance of MyFragmentActivity, the keyword this is still usable, you just have to tell it which "this" using MyFragmentActivity.this. Note that this is the class name, not an instance name.

于 2012-07-26T02:46:18.627 回答