简体   繁体   English

来自 Android 中不同 Class 的 setContentView(R.layout.xy)

[英]setContentView(R.layout.xy) from different Class in Android

I made an app from different tutorials and have one main problem.我根据不同的教程制作了一个应用程序,但有一个主要问题。

Normally I set all my layout views in the MainActivity.java class:通常我在 MainActivity.java class 中设置我的所有布局视图:

public class MainActivity extends AppCompatActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.layoutA);
    }

public void whenClicked (View view){
    setContentView(R.layout.layoutB);
    }

// and so on..
}

At some point in MainActivity.java I call a second class which does some mysql stuff for me and now I want this class to setContentView(R.layout.layoutC) if it was successful. At some point in MainActivity.java I call a second class which does some mysql stuff for me and now I want this class to setContentView(R.layout.layoutC) if it was successful.

Here ist the part from MainActivity:这是 MainActivity 的部分:

BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, dataA, dataB, dataC, dataD);

And this is the head of the BackgroundWorker.java where I want to set the ContentView from:这是 BackgroundWorker.java 的头,我想在其中设置 ContentView:

public class BackgroundWorker extends AsyncTask<String, Void, String> {

    Context context;

    BackgroundWorker(Context ctx){
        context = ctx;
    }

    @Override
    protected String doInBackground(String... params) {

         // do some mysql stuff..
    }
}

Would be great if anyone could give me a hint how to do this in my case.如果有人能在我的情况下给我一个提示如何做到这一点,那就太好了。 I tried a lot of suggestions from stackoverflow but nothing worked out yet.我尝试了很多来自 stackoverflow 的建议,但还没有解决。

Thank you!谢谢!

Three hints:三个提示:

Add a Calback parameter to your AsyncTask.将 Calback 参数添加到您的 AsyncTask。

Or add an interface.或者添加一个接口。

Or overwrite onPostExecute in main activity.或覆盖主要活动中的 onPostExecute。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM