Reputation: 1219
I am working on an Android app to handle workorders for technicians in the field
First let me explain the layout of my app:
The main part of the app contains 3 tabs
Tab 1 - Home Tab
-contains a summary of the current workorders that are open
Tab 2 - Assigned Workorders Tab
-contains a list of workorders that are currently assigned to the user who logged in
Tab 3 - Unassigned Workorders Tab
-contains a list of workorders that are assigned to the user's crew, but not to a specific person
I use a class called "Database" that is a Singleton class that is part of the Application context so that it can be accessed throughout the entire app. I will include some code from it below
Here's the issue:
When the user click a particular workorder in the list, a workorder detail activity launches displaying all kinds of information about the workorder. If the workorder is an "unassigned" one, it also displays a "Take Ownership" button that changes the "owner" field to the current user, removes it from the unassigned list, and adds it to the assigned list for that user. However, once the user hits the back button to close the workorder detail screen(activity), and goes back to the tabs view, the lists do not update until you somehow force the view to refresh (such as scrolling farther down on the list so that the workorder that just changed ownership is off the screen).
I have tried:
-moving the custom ListAdapter into the Database (singleton) class and accessing it through there
-calling "notifyDataSetChanged()" on the list adapters
Does anyone have any ideas for how to get it to update each time a tab is selected (or the workorder detail screen is dismissed by hitting the back button)?? I've been scouring the internet trying to find a solution and have not succeeded with anything I found, so I post here once again, knowing that most people here are smarter than me!
Thanks in advance for your help! I have included relevant code below:
Database class:
public class Database extends Application {
private static Database instance = null;
private ArrayList<Workorder> assignedwolist;
private ArrayList<Workorder> unassignedwolist;
private Credentials creds;
private WOListAdapter assignedadapter;
private WOListAdapter unassignedadapter;
private static void checkInstance() {
if(instance==null) {
throw new IllegalStateException("Database class not created yet!");
}
}
@Override
public void onCreate() {
super.onCreate();
instance=this;
}
public Database() {
this.creds=null;
this.assignedwolist = getAssignedWorkorders();
this.unassignedwolist = getUnassignedWorkorders();
this.assignedadapter=new WOListAdapter(this,getAssignedWorkorders());
this.unassignedadapter=new WOListAdapter(this,getUnassignedWorkorders());
}
public ArrayList<String> getSummary() {
int numassigned = assignedwolist.size();
int assignedp1 = 0;
for(Workorder wo:assignedwolist) {
if(wo.getPriority().equalsIgnoreCase("1")) {
assignedp1++;
}
}
int numunassigned = unassignedwolist.size();
int unassignedp1 = 0;
for(Workorder wo:unassignedwolist) {
if(wo.getPriority().equalsIgnoreCase("1")) {
unassignedp1++;
}
}
ArrayList<String> summary = new ArrayList<String>();
summary.add(Integer.toString(numassigned));
summary.add(Integer.toString(assignedp1));
summary.add(Integer.toString(numunassigned));
summary.add(Integer.toString(unassignedp1));
return summary;
}
public Workorder getWO(String id) {
for(Workorder wo:assignedwolist) {
if(wo.getId().equalsIgnoreCase(id)) {
return wo;
}
}
for(Workorder wo:unassignedwolist) {
if(wo.getId().equalsIgnoreCase(id)) {
return wo;
}
}
return null;
}
public ArrayList<Workorder> getAssignedWorkorders() {
return assignedwolist;
}
public ArrayList<Workorder> getUnassignedWorkorders() {
return unassignedwolist;
}
public void setValidated(boolean validated) {
this.validated = validated;
}
public boolean isValidated() {
return validated;
}
public void takeOwnership(String wonum) {
Workorder wo;
for(int i=0;i<unassignedwolist.size();i++) {
if(unassignedwolist.get(i).getId().equalsIgnoreCase(wonum)) {
wo = unassignedwolist.remove(i);
wo.setOwner(creds.getUsername());
assignedwolist.add(wo);
Collections.sort(assignedwolist);
return;
}
}
this.assignedadapter.notifyDataSetChanged();
this.unassignedadapter.notifyDataSetChanged();
}
public WOListAdapter getAssignedAdapter() {
return assignedadapter;
}
public WOListAdapter getUnassignedAdapter() {
return unassignedadapter;
}
}
Main App (contains Tabs):
public class MainApp extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
/*Tab host for tabs */
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);// The activity TabHost
TabSpec hometabspec = tabHost.newTabSpec("Home");
TabSpec assignedtabspec = tabHost.newTabSpec("Assigned");
TabSpec unassignedtabspec = tabHost.newTabSpec("Unassigned");
hometabspec.setIndicator("Home",getResources().getDrawable(R.drawable.ic_tab_home)).setContent(new Intent(this,HomeActivity.class));
assignedtabspec.setIndicator("Assigned",getResources().getDrawable(R.drawable.ic_tab_assigned)).setContent(new Intent(this,AssignedActivity.class));
unassignedtabspec.setIndicator("Unassigned",getResources().getDrawable(R.drawable.ic_tab_unassigned)).setContent(new Intent(this,UnassignedActivity.class));
tabHost.addTab(hometabspec);
tabHost.addTab(assignedtabspec);
tabHost.addTab(unassignedtabspec);
tabHost.getTabWidget().setCurrentTab(0);
}
}
AssignedActivity:
public class AssignedActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Database db = (Database)getApplicationContext();
setContentView(R.layout.wolist);
ListView listview =(ListView)findViewById(R.id.wolistview);
listview.setTextFilterEnabled(false);
WOListAdapter wolistadapter = db.getAssignedAdapter();
listview.setAdapter(wolistadapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterview, View view, int position,
long arg3) {
Workorder wo = (Workorder)adapterview.getItemAtPosition(position);
Intent i = new Intent();
i.setClass(AssignedActivity.this,WorkorderDetailActivity.class);
i.putExtra("wonum",wo.getId());
startActivity(i);
}
});
}
}
UnassignedActivity:
public class UnassignedActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Database db = (Database)getApplicationContext();
setContentView(R.layout.wolist);
ListView listview =(ListView)findViewById(R.id.wolistview);
listview.setTextFilterEnabled(false);
WOListAdapter wolistadapter = db.getUnassignedAdapter();
listview.setAdapter(wolistadapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterview, View view, int position,
long arg3) {
Workorder wo = (Workorder)adapterview.getItemAtPosition(position);
Intent i = new Intent();
i.setClass(UnassignedActivity.this,WorkorderDetailActivity.class);
i.putExtra("wonum",wo.getId());
startActivity(i);
}
});
}
}
WorkorderDetailActivity:
public class WorkorderDetailActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Database db = (Database)getApplicationContext();
final Workorder wo;
Bundle extras = getIntent().getExtras();
if(extras!=null) {
//Set Fields
wo = db.getWO(extras.getString("wonum"));
setContentView(R.layout.wodetail);
TextView tv=(TextView)findViewById(R.id.wonumview);
tv.setTextSize(20);
tv.setText(wo.getId());
tv=(TextView)findViewById(R.id.wodescriptionview);
tv.setTextSize(18);
tv.setText(wo.getDescription());
tv=(TextView)findViewById(R.id.wolocationview);
tv.setTextSize(18);
tv.setText(wo.getLocation());
tv=(TextView)findViewById(R.id.wostatusview);
tv.setTextSize(18);
tv.setText(wo.getStatus());
tv=(TextView)findViewById(R.id.woreporteddateview);
tv.setTextSize(18);
tv.setText(wo.getReportdate().substring(0, 19));
tv=(TextView)findViewById(R.id.wotypeview);
tv.setTextSize(18);
tv.setText(wo.getType());
//Assigned or Unassigned adjustments
tv=(TextView)findViewById(R.id.woownerview);
Button b = (Button)findViewById(R.id.TakeOwnershipButton);
if(wo.getOwner().equalsIgnoreCase("none")) { //Display TakeOwnership button
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Database db = (Database)getApplicationContext();
db.takeOwnership(wo.getId());
Button b = (Button)findViewById(R.id.TakeOwnershipButton);
TextView tv=(TextView)findViewById(R.id.woownerview);
b.setVisibility(View.INVISIBLE);
tv.setVisibility(View.VISIBLE);
tv.setTextSize(18);
tv.setText(wo.getOwner());
}
});
b.setVisibility(View.VISIBLE);
tv.setVisibility(View.INVISIBLE);
} else { //Display owner
b.setVisibility(View.INVISIBLE);
tv.setVisibility(View.VISIBLE);
tv.setTextSize(18);
tv.setText(wo.getOwner());
}
//Icon color adjustment based on priority
ImageView icon = (ImageView)findViewById(R.id.woiconview);
switch(wo.getPriorityNum()) {
case 1: icon.setColorFilter(Color.RED); break;
case 2: icon.setColorFilter(Color.YELLOW); break;
case 3: icon.setColorFilter(Color.GREEN); break;
case 4: icon.setColorFilter(Color.BLUE); break;
default: icon.setColorFilter(Color.TRANSPARENT); break;
}
//Status change options
Spinner s = (Spinner)findViewById(R.id.wostatusselector);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, db.getValidStatuses(wo.getStatus()));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
} else { //No wonum was passed in!
TextView tv = new TextView(this);
tv.setText("Error Retrieving Workorder");
setContentView(tv);
}
}
}
Upvotes: 2
Views: 1550
Reputation: 12823
I use setOnTabChanged() to update Google Analytics and request a new add from AdMob:
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
tracker.dispatch();
adView.requestFreshAd();
}});
Upvotes: 2