简体   繁体   中英

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:

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.

Here ist the part from 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:

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.

Thank you!

Three hints:

Add a Calback parameter to your AsyncTask.

Or add an interface.

Or overwrite onPostExecute in main activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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