0

So this project is driving me insane. Thank you to Ahmed Aeon Axan for the last answer. I have never worked with HashTables before but from the all the code I've looked at this should be working. Please tell me why this is not displaying in my listview.

Created in the model.java below

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 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 Set<String> newLocArr = new HashSet<String>(locations);
private SimpleDateFormat sdf = new SimpleDateFormat("E, MMM dd");
private ArrayList<String> times = 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 ArrayList<String> getDates() {
    for (int i = 0; i < datesSmoked.size(); i++) {
        String s = (sdf.format(i));
        times.add(s);
    }
    return times;
}

public List<String> getPlacesSmoked() { 

    for (String key : locations) {

    newLocArr.add(key+ ": " + Collections.frequency(locationsSmoked, key)); 

    }       
    return new ArrayList<String>(newLocArr);
}

public ArrayList<String> getAllIncidentsArray() {

    for (int i = 0; i < datesSmoked.size(); i++) {
        allIncidents.add(getDates().get(i) + ", " + locationsSmoked.get(i));
    }

    return allIncidents;
}

public ArrayList<String> getlocationsArray() {
    return this.locations;
}

public ArrayList<String> getLocationsSmokedArray() {
    return this.locationsSmoked;
}

public ArrayList<GregorianCalendar> getDatesSmokedArray() {
    return this.datesSmoked;
}

Ends the relevant code for model.java

called into the list view in the Locations Activity below where it is not displaying

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.getPlacesSmoked());      
    listView.setAdapter(listAdapter);
    listAdapter.notifyDataSetChanged();
}

Essentially Im trying to get the ArrayList locationsSmoked which Displays
Home
Home
Home
Home
School
School
School
School

to display
Home: 4
School: 4

Mat
  • 202,337
  • 40
  • 393
  • 406
IrishWhiskey
  • 339
  • 5
  • 27
  • I edited my post to show the final working code, got rid of the set method that was absolutely not needed. Thanks again for all the help. I hope this helps anyone else with a similar problem – IrishWhiskey Mar 11 '13 at 05:15

1 Answers1

0

Your locTest list is empty, since it is initialized at the Model creation with empty HashSet test1 which is initialized with empty locations list.

The List(Collection<?>) constructor is copying the values, not the pointer to the collection, as far as I remember

fast solution (not sure if it do the trick actually):

public Model(GregorianCalendar date){
    startDate = date;
    for (String s : this.defaultLocations) {            
        locations.add(s);
    }           
    // calling setPlacesSmoked to process data
    setPlacesSmoked();
}

public void setPlacesSmoked() {
    // assuming that locations list holds the data needed to process
    for (String key : locations) {
        test1.add(key+ ": " + Collections.frequency(locations, key));           
    }
}


public List<String> getPlacesSmoked() {             
    //return locTest;
    return new ArrayList<String>(test1);
}

The expected output:

Home: 1
Work: 1 
Commuting: 1
School: 1
Bar: 1
Restaurant: 1
Social Gathering: 1
Other: 1

But that depends on the locations contents

Mykhailo Gaidai
  • 3,130
  • 1
  • 21
  • 23
  • The locations `ArrayList` is filled initially by the defaultLocations `Array` in my model constructor. Then in the application it is also added to, and modified in other activities. However you may be right about the locTest `ArrayList` being empty. However I'm attempting to set test1 in the setPlacesSmokedMethod() then convert it to an `ArrayList` at the top. Am I doing this wrong like you say? If so could you post a quick solution to show me how I'm doing this wrong? – IrishWhiskey Mar 11 '13 at 00:36
  • I've added kind of fast solution to the answer. Basic idea is to move the initialization of your `HashSet` and test list AFTER the base `List` is populated. Please take a look – Mykhailo Gaidai Mar 11 '13 at 00:42
  • no but you may be on to something. It is now displaying the original "locations" or "default locations" `array`/`arraylist` in my adapter, in my locations `activity`. So in essence you are right that my list is empty, because whatever I'm doing in my set method is not returning in my get method – IrishWhiskey Mar 11 '13 at 00:46
  • I'm sorry, but I don't get the general idea. As far as I understand, you are trying to display some values with corresponding frequency in the `ListView`? the method `setPlacesSmoked` is not been called anywhere(which should modify the `HashSet`, right?) – Mykhailo Gaidai Mar 11 '13 at 00:53
  • Well [this post](http://stackoverflow.com/questions/15326944/string-arraylist-using-collections-android) is what originally led me to this solution. Maybe reading it will help you understand. Sorry for making it confusing and thank you for the help – IrishWhiskey Mar 11 '13 at 00:55
  • Ok, I think I've got it. **First of all**, you should have a list of places BEFORE the `setPlacesSmoked` call(right now, you have all of the locations appearing once). **Then** you should fix `setPlacesSmoked`, since now you are walking through and trying to modify the same list. And **finally** you should call fixed `setPlacesSmoked` and it will fill your `HashSet` with correct places/frequencies pair. And display issue is already solved :) – Mykhailo Gaidai Mar 11 '13 at 01:01
  • oh boy, ok. Well I'll give it a shot, my brain is just fried at this point. If you feel like modifying your code above to show what your saying that would be amazing, but you've done enough already so don't feel obligated. I'll implement the steps you gave and accept your answer asap – IrishWhiskey Mar 11 '13 at 01:04