ANDROID:带有标签的应用程序,用于显示列表中项目的数量,仅在关闭并重新打开应用程序时才会更新 [英] ANDROID:app with tab which shows the count of number of items in the list gets updated only when closing and reopening the app

查看:45
本文介绍了ANDROID:带有标签的应用程序,用于显示列表中项目的数量,仅在关闭并重新打开应用程序时才会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class overview extends TabActivity {

private MyApplication app;
private JSONArray v;
private TabWidget m_tabs;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_overview);
    app = ((MyApplication) getApplicationContext());
    app.ActivityMode = true;
    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(gvendorservice.msgID);
    int pending=0;
    int delivered=0;

    TabHost tabHost = getTabHost();
    // Tab for ShowOrders
    TabSpec ShowOrders = tabHost.newTabSpec("Normal Order");
    // setting Title and Icon for the Tab
    ShowOrders.setIndicator("Normal Order");
    Intent ShowOrdersIntent = new Intent(this, Orders.class);
    ShowOrders.setContent(ShowOrdersIntent);

    // Tab for Showchatlist
    TabSpec Showchatlist = tabHost.newTabSpec("Chat");
    Intent ShowchatlistIntent = new Intent(this, activity_chatlist.class);
    Showchatlist.setIndicator("CHATS\n\t"+app.getNewMsgCount().length());
    Showchatlist.setContent(ShowchatlistIntent);

    // Tab for AcceptOrder
    TabSpec AcceptOrder = tabHost.newTabSpec("Order");
    Intent AcceptOrderIntent = new Intent(this, activity_acceptorder.class);
    AcceptOrder.setIndicator("OrderS\n\t"+app.getOpenOrders().length());
    AcceptOrder.setContent(AcceptOrderIntent);

    //Tab for MyChatOrder
    TabSpec MyChatOrder = tabHost.newTabSpec("Pending");

    Intent MyChatOrdertIntent = new Intent(this, activity_mychatorders.class);
    MyChatOrdertIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    JSONArray orderlistArray = app.getMyChatOrders();
    try {
        TextView tcp = (TextView) findViewById(R.id.txtChatOrdersPending);
        for (int i = 0; i < orderlistArray.length(); i++) {
            JSONObject cat = orderlistArray.getJSONObject(i);
            if (cat.getInt("delivered") == 0) {
                pending++;
            } else {
                delivered++;
            }
        }

        tcp.setText(String.valueOf(pending));
    }
    catch(JSONException e)
    {

    }

    MyChatOrder.setIndicator("Pending\n\t"+String.valueOf(pending));
    MyChatOrder.setContent(MyChatOrdertIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(ShowOrders); // Adding normal order tab
    tabHost.addTab(Showchatlist); // Adding chat tab
    tabHost.addTab(AcceptOrder); // Adding orders tab
    tabHost.addTab(MyChatOrder);//Adding pending tab
    for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
        TabWidget tw = (TabWidget) tabHost.findViewById(android.R.id.tabs);
        View tabView = tw.getChildTabViewAt(i);
        TextView tv = (TextView) tabView.findViewById(android.R.id.title);
        tv.setTextSize(12);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setAllCaps(true);
    }
}

@Override
protected void onResume() {
    super.onResume();
    app.ActivityMode = true;
    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(gvendorservice.msgID);
    int pending=0;
    int delivered=0;

    JSONArray orderlistArray = app.getMyChatOrders();
    try {

        TextView tcp = (TextView) findViewById(R.id.txtChatOrdersPending);
        for (int i = 0; i < orderlistArray.length(); i++) {
            JSONObject cat = orderlistArray.getJSONObject(i);
            if (cat.getInt("delivered") == 0) {
                pending++;
            } else {
                delivered++;
            }
        }

        tcp.setText(String.valueOf(pending));
    }
    catch(JSONException e)
    {
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    app.ActivityMode = false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_overview, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    Intent intent;
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_logout) {
        app = ((MyApplication) getApplicationContext());
        app.logOut();
        intent = new Intent(overview.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        overview.this.startActivity(intent);
        overview.this.finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

我能够获取聊天订单和待处理列表的更新计数。但这仅在我关闭并重新打开该应用程序时才会发生。可能是什么问题?

I am able to get the updated count of chats orders and pending list. But this happens only when I close and reopen the app. What might be the problem?

推荐答案

这是因为所有代码都在所谓的生命周期方法中执行。这些方法(onCreate(),onStart(),onResume(),onDestroy()等)描述了首次启动,恢复或关闭活动(大致而言)时发生的步骤。包含用于更新列表的代码的方法是onResume(),它仅在每次启动活动或通过打开应用程序恢复活动时才被调用。如果您想定期更新数据,则应尝试使用后台服务,AsyncTask或某种后台线程

This is because all your code is being executed within what's known as Lifecycle methods. These methods (onCreate(), onStart(), onResume(), onDestroy() to name a few) describe the steps that happen when an activity is launched for the first time, resumed, or closed (roughly speaking). The method that contains the code to update the list is onResume() which only gets called every time an activity is either started or of course, resumed via opening the app. If you want data to be updated periodically then you should try using a background service, AsyncTask or background thread of some kind

这篇关于ANDROID:带有标签的应用程序,用于显示列表中项目的数量,仅在关闭并重新打开应用程序时才会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆