问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/42083909/androidapp-with-tab-which-shows-the-count-of-number-of-items-in-the-list-gets-u