2013-03-11 44 views
0

所以這個項目讓我瘋狂。感謝Ahmed Aeon Axan提供的最後答案。我從來沒有使用HashTables之前,但從我看過的所有代碼應該工作。請告訴我爲什麼這不顯示在我的listview中。HashSet ArrayList不在列表視圖中顯示

在model.java創建以下

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

結束對model.java

相關的代碼中調用到了活動地點如下它沒有顯示

列表視圖
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(); 
} 

本質上我試圖得到ArrayList locationsSmoked這顯示
首頁
首頁
首頁
首頁
學校
學校
學校
學校

顯示
主頁:4
學校:4

+0

非常感謝你m1shk4 – IrishWhiskey 2013-03-11 01:14:43

+0

我編輯了我的帖子以顯示最終的工作代碼,擺脫了絕對不需要的設置方法。再次感謝所有的幫助。我希望這可以幫助其他類似問題的人 – IrishWhiskey 2013-03-11 05:15:48

回答

0

locTest您的locTest列表爲空,因爲它在Model創建時初始化爲空HashSettest1這是用空locations列表初始化的。

List(Collection<?>)構造函數的值複製,而不是指向集合,據我記得

快速的解決方案(如果它做的伎倆居然不知道):

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

預期產出:

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

但這取決於locations內容

+0

位置'ArrayList'最初由我的模型構造函數中的defaultLocations'Array'填充。然後在應用程序中,它也被添加到其他活動中並進行修改。然而你可能是正確的locTest'ArrayList'是空的。不過,我試圖在setPlacesSmokedMethod()中設置test1,然後將它轉換爲頂部的'ArrayList'。我是否像你說的那樣做錯了?如果是的話,你可以發佈一個快速解決方案,告訴我我是如何做錯了? – IrishWhiskey 2013-03-11 00:36:50

+0

我已經爲答案添加了一種快速解決方案。基本思路是在填充基礎List之後移動你的'HashSet'和測試列表的初始化。請看看 – 2013-03-11 00:42:20

+0

不,但你可能會做些什麼。它現在在我的適配器中,在我的位置「活動」中顯示原始「位置」或「默認位置」陣列/陣列列表。所以實質上你是對的,我的列表是空的,因爲無論我在我的set方法中做什麼都沒有返回我的get方法 – IrishWhiskey 2013-03-11 00:46:01