My main activity
has multiple ArrayLists
and Arrays
. When you click the Smoked Cigarette button, the application logs certain information in the ArrayList
. One of the ArrayLists
locationsSmoked holds all the locations you've smoked at. My task is to make an activity
that displays in a ListView
the location you smoked at, and how many times you smoked there.
If you display the locationsSmoked array
currently it will look like this
Home
Home
Home
Work
Work
School
School
and so on and so forth. I need to loop through this array
to display the amount of times each string
is called and give the integer
value. I then need to combine the integer
with the matching string
. In a parallel array
or multidimensional array
to display the information in another activity
named Locations in that activity
's listview
So it looks like this
Home: 3
Work: 2
School: 2
It should be noted that even though you should never hard code elements, in this situation I literally am not allowed to because the ArrayList
of locations will be constantly growing. There is another activity
I've already completed that allows the user to add new locations to the existing ArrayList
I think the code I'm about to list below from my model.java and my locations.java is for the most part accurate. However nothing displays in the listview
when i go to the activity
.
So if that was confusing in any please let me know and I'll ask more specifically.
Thanks for any help you can offer.
Model.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
public class Model implements Serializable {
public static final int END_MORNING = 11; // 11:00AM, inclusive
public static final int END_AFTERNOON = 16; // 4:00PM, inclusive
private int locNumber;
private String locName;
private GregorianCalendar startDate;
private ArrayList<GregorianCalendar> datesSmoked = new ArrayList<GregorianCalendar>();
private ArrayList<String> locationsSmoked = new ArrayList<String>();
private ArrayList<String> locations = new ArrayList<String>();
private ArrayList<String> allIncidents = new ArrayList<String>();
private ArrayList<String> test = new ArrayList<String>();
public String [] defaultLocations = {"Home", "Work", "Commuting", "School", "Bar", "Restaurant", "Social Gathering", "Other"};
public String [] eachSmoked;
public Model(GregorianCalendar date){
startDate = date;
for (String s : this.defaultLocations) {
locations.add(s);
}
}
public Model(){
this(new GregorianCalendar()); // now
}
public void setAllIncidentsArray() {
this.allIncidents.add(datesSmoked.toString());
this.allIncidents.add(locationsSmoked.toString());
}
public void setEachSmoked() {
for (int i = 0; i < locationsSmoked.size(); i++) {
locNumber = Collections.frequency(locationsSmoked, locationsSmoked.get(i));
locName = locations.get(i);
test.add(i, locNumber + locName);
}
}
public ArrayList<String> getEachSmoked() {
return this.test;
}
public ArrayList<String> getAllIncidentsArray() {
return this.allIncidents;
}
public ArrayList<String> getlocationsArray() {
return this.locations;
}
public ArrayList<String> getLocationsSmokedArray() {
return this.locationsSmoked;
}
public ArrayList<GregorianCalendar> getDatesSmokedArray() {
return this.datesSmoked;
}
public void incrementCount(String location) {
this.datesSmoked.add(new GregorianCalendar()); // now
this.locationsSmoked.add(location);
}
public int getTodayCount() {
GregorianCalendar now = new GregorianCalendar();
int todayDayOfYear = now.get(Calendar.DAY_OF_YEAR);
int count = 0;
for (GregorianCalendar day : this.datesSmoked)
if (day.get(Calendar.DAY_OF_YEAR) == todayDayOfYear)
count++;
return count;
}
public int getTotalCount() {
return this.datesSmoked.size();
}
public double getAverage() {
if (getDays() > 0)
return (double) getTotalCount() / getDays();
else
return 0.0;
}
public int getDays() {
return (new GregorianCalendar()).get(Calendar.DAY_OF_YEAR) - startDate.get(Calendar.DAY_OF_YEAR) + 1;
}
public int getMorning() {
int count = 0;
for (GregorianCalendar date : this.datesSmoked)
if (date.get(Calendar.HOUR_OF_DAY) <= END_MORNING)
count++;
return count;
}
public int getAfternoon() {
int count = 0;
for (GregorianCalendar date : this.datesSmoked)
if (date.get(Calendar.HOUR_OF_DAY) > END_MORNING && date.get(Calendar.HOUR_OF_DAY) <= END_AFTERNOON)
count++;
return count;
}
public int getEvening() {
int count = 0;
for (GregorianCalendar date : this.datesSmoked)
if (date.get(Calendar.HOUR_OF_DAY) > END_AFTERNOON)
count++;
return count;
}
}
Locations.java (locations Activity)
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class LocationActivity extends Activity {
public static final String SMOKIN_DATA_FILE = "smokin.dat";
public static Model model = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
restoreModel();
ListView listView = (ListView) findViewById(R.id.location_listview_Id);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, model.getEachSmoked());
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_location, menu);
return true;
}
public void restoreModel() {
// Restore from disk, or start with an empty model
try {
ObjectInputStream ois = new ObjectInputStream(
openFileInput(SMOKIN_DATA_FILE));
model = (Model) ois.readObject();
ois.close();
} catch (Exception e) {
Log.v("*** DEBUG ***", "Error writing to file: " + e);
model = new Model();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home_Id:
Intent homeIntent = new Intent(this, MainActivity.class);
startActivity(homeIntent); ;
return true;
case R.id.menu_all_incidents_Id:
Intent allIncidentsIntent = new Intent(this, AllIncidentsActivity.class);
startActivity(allIncidentsIntent); ;
return true;
case R.id.menu_edit_locations_Id:
Intent editLocationsIntent = new Intent(this, EditLocationsActivity.class);
startActivity(editLocationsIntent); ;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}