简体   繁体   English

使用AsyncTask加载listview

[英]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. 我有以下情况要解决:当向用户显示初始屏幕(SplashScreenActivity.java)时,我想填充列表视图(MainActivity.java)。 I would like to use AsyncTask to perform this action but i don't know exactly how to use it in this case: 我想使用AsyncTask执行此操作,但是在这种情况下我不知道该如何使用:

SplashScreen.java 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. MainActivity.java只是一个列表视图,它使用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(); 您需要在SplashActivity之前打开MainActivity ,在MainActivity您需要从服务器获取数据并加载列表,而在获取过程中,您打开SplashActivity并调用您所使用的相同PostDelayed方法,但是在运行时仅调用finish(); and it will be back to MainActivity hopefully loaded. 并有望返回MainActivity

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. 请注意,您假设用户的互联网连接速度足够快,可以在1500毫秒内加载数据,这种情况的理想选择是使用基于事件的应用程序,寻找EventBus来实现。

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

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