简体   繁体   中英

Loading listview using AsyncTask

I have the following case to solve: I want to populate my listview (MainActivity.java) when splash screen (SplashScreenActivity.java) is displayed to the user. I would like to use AsyncTask to perform this action but i don't know exactly how to use it in this case:

SplashScreen.java

public class SplashScreenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);



    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);

            startActivity(intent);


        }
    }, 1500);

}

MainActivity.java is just a listview which shows data using JSON.

Dont listen to this comments.

You need to open MainActivity before SplashActivity , in MainActivity you need to fetch the data from the server and load the list, while it is fetching you open the SplashActivity and calls the same PostDelayed method that you are using but when it runs just calls finish(); and it will be back to MainActivity hopefully loaded.

Be aware that you are assuming that the internet connection of the user is fast enough to load the data in 1500 miliseconds, the ideal of this situation is using a Event based application, look for EventBus to achieve that.

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