0

所以这个项目快把我逼疯了。感谢 Ahmed Aeon Axan 的最后回答。我以前从未使用过,HashTables但从我看过的所有代码来看,这应该可以工作。请告诉我为什么这没有显示在我的listview.

在下面的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显示
Home
Home
Home
Home
School
School
School
School的位置Smoked

显示
家庭:4
学校:4

4

1 回答 1

0

您的locTest列表是空的,因为它在Model创建时使用空列表HashSet test1进行初始化,而该列表使用空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内容

于 2013-03-11T00:29:41.983 回答