1

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);
    }

}
}
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
IrishWhiskey
  • 339
  • 5
  • 27

1 Answers1

1

This looks like a perfect oportunity to use a Hashtable<String, Integer>, The Hashtable would contain the locations as the Keys and Count as the values.

Now when you generate the data for the ListView, simply iterate over the ArrayList elements for places smoked, if the element is not in the HashTable add it and set Value to 0. If it is already in it simply increment it by one.

At the end of it, you have a HashTable with all the data you need.

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
  • thank you very much, I'm going to attempt your solution and user370305's solution before accepting an answer. – IrishWhiskey Mar 10 '13 at 20:24
  • I have never used `HashTables`, I looked at the documentation, and honestly its a bit confusing. Your answer seems like it would do exactly what I need though. Would you be willing to post a quick pseudo code example on how to implement it? I'm a bit confused on whether I need to take out my `ArrayList`, or overwrite it, and how that would effect the rest of my `activities` still using that `ArrayList` – IrishWhiskey Mar 10 '13 at 21:29
  • So I searched for an example of `hashtable` doing exactly what you suggested and was rewarded with my pseudo code [here](http://stackoverflow.com/questions/5211194/count-occurences-of-words-in-arraylist). I'm resourceful, and that last comment was not fair to you Ahmed, you shouldn't have to do anymore work. That was a perfectly good answer. Accepted and thanked! – IrishWhiskey Mar 10 '13 at 22:00
  • No problem really, I would have gladly helped you out with some pseudocode. It just happened that it was midnight at my place when you had commented. Anyways I'm glad you solved your problem. – Ahmed Aeon Axan Mar 11 '13 at 12:17