2017-02-07 10 views
0
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); 
} 

我能夠獲得更新的聊天訂單和待處理列表的計數。但是,只有當我關閉並重新打開應用程序時纔會發生這種情況。可能是什麼問題?ANDROID:帶有選項卡的應用程序,顯示列表中項目數的計數僅在關閉並重新打開應用程序時更新

回答

0

這是因爲您的所有代碼都在所謂的生命週期方法中執行。這些方法(onCreate(),onStart(),onResume(),onDestroy()僅舉幾個例子)描述了第一次啓動,恢復或關閉(粗略地說)活動時發生的步驟。包含更新列表的代碼的方法是onResume(),每次開始活動時都會調用onResume(),或者通過打開應用程序繼續進行。如果你想定期更新數據,那麼你應該嘗試使用後臺服務,AsyncTask或某種後臺線程

+0

你能幫我給一個示例代碼來做到這一點。我是新來的android編碼 – tiji

相關問題