[英]setContentView(R.layout.xy) from different Class in Android
我根據不同的教程制作了一個應用程序,但有一個主要問題。
通常我在 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.
這是 MainActivity 的部分:
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, dataA, dataB, dataC, dataD);
這是 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..
}
}
如果有人能在我的情況下給我一個提示如何做到這一點,那就太好了。 我嘗試了很多來自 stackoverflow 的建議,但還沒有解決。
謝謝!
三個提示:
將 Calback 參數添加到您的 AsyncTask。
或者添加一個接口。
或覆蓋主要活動中的 onPostExecute。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.