【问题标题】:Tabbar with Async Task in AndroidAndroid中带有异步任务的标签栏
【发布时间】:2012-05-03 15:58:03
【问题描述】:

我已经构建了一个应用程序,一旦应用程序第一次运行,它就会开始将图像和一些数据从应用程序复制到内部存储器。然后在其他活动的其他地方使用这些数据。

复制过程需要 30 多秒。所以我想在异步任务中做到这一点。但我无法让它工作。异步任务运行良好,但我需要创建选项卡规范并将意图分配给 onPostExecute 方法上的每个规范。但我看到的只是没有规格的标签栏,没有任何效果。

那么知道如何以这种方式构建它吗?

任何帮助将不胜感激。 这是我的活动,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabbar_main);

    myJSON = new DataBaseJSONFunctions(this);

    TabHost tabHost = getTabHost();

    TabWidget tabWidget = tabHost.getTabWidget();

    Resources res = getResources();
    //TabHost tabHost = getTabHost();

    TabHost.TabSpec spec;
    Intent intent;

    // Starting Initialization process.
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if(!prefs.getBoolean("firstRun", false)) {

        initializeAll();
        // Set "firstRun" true to make it runs once application is installed.
        prefs.edit().putBoolean("firstRun", true).commit();
    }

我想把这个块放在一个异步任务中,

    intent = new Intent().setClass(this,Activities.class);
    spec = tabHost.newTabSpec("activities").setIndicator("act",res.getDrawable(R.drawable.tab_activities_selector)).setContent(intent);
    tabHost.addTab(spec);

    // doing the same things.
    intent = new Intent().setClass(this, Promotions.class);
    spec = tabHost.newTabSpec("promotion").setIndicator("Promotion",res.getDrawable(R.drawable.tab_promotions_selector)).setContent(intent);
    tabHost.addTab(spec);


    // doing the same things for Albums Activity.
    intent = new Intent().setClass(this,Menu.class);
    spec = tabHost.newTabSpec("menu").setIndicator("Menü",res.getDrawable(R.drawable.tab_menu_selector)).setContent(intent);
    tabHost.addTab(spec);

    for(int i = 0; i < tabWidget.getChildCount(); i++){
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bar);
    }

    tabHost.setCurrentTab(0);

顺便说一句,我正在将我的活动上下文传递给异步任务。

【问题讨论】:

    标签: android tabs android-asynctask android-tabhost


    【解决方案1】:

    尝试从AsyncTask 填充TabHost 有点不合常规。我建议在您的应用程序启动后立即使用选项卡填充TabHost,然后运行AsyncTask,它可能会或可能不会显示ProgressDialog,让用户知道正在发生的事情,然后在您的@987654326 @ 已完成,您可以用新数据填充您的 UI。

    【讨论】:

    • 我会尽快尝试,但问题是如果我在应用程序启动后立即填充标签栏,我会怎么做?我的意思是我需要从 AsyncTask 返回的数据来填充 UI。如果我在没有数据的情况下启动意图,我将得到空指针。
    • 您可以使用上次运行中保存的数据,或者如果这是您的第一次运行,则可以使用手动制作的占位符数据。关键是,你的 UI 应该显示一些东西,然后你可以开始后台更新,并且可能显示 ProgressBar 来告诉用户正在发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多