【问题标题】:HashSet to ArrayList not Displaying in ListviewHashSet 到 ArrayList 不在 Listview 中显示
【发布时间】:2013-03-11 00:08:30
【问题描述】:

所以这个项目让我发疯了。感谢 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();
}

基本上我试图获取显示的ArrayListlocationsSmoked
首页
首页
首页
首页
学校
学校
学校
学校

显示
主页:4
学校:4

【问题讨论】:

  • 非常感谢 m1shk4
  • 我编辑了我的帖子以显示最终的工作代码,摆脱了绝对不需要的 set 方法。再次感谢所有帮助。我希望这可以帮助其他有类似问题的人

标签: android listview arraylist


【解决方案1】:

您的locTest 列表是空的,因为它在Model 创建时使用空HashSet test1 初始化,而locations 列表初始化为空。

据我所知,List(Collection&lt;?&gt;) 构造函数正在复制值,而不是指向集合的指针

快速解决方案(不确定它是否真的有效):

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 的内容

【讨论】:

  • 位置 ArrayList 最初由我的模型构造函数中的 defaultLocations Array 填充。然后在应用程序中,它也被添加到其他活动中,并在其他活动中进行修改。但是,您可能对 locTest ArrayList 为空是正确的。但是我试图在 setPlacesSmokedMethod() 中设置 test1 然后将其转换为顶部的 ArrayList 。我像你说的那样做错了吗?如果是这样,您能否发布一个快速解决方案,向我展示我做错了什么?
  • 我为答案添加了一种快速解决方案。基本思想是在填充基础List 之后移动HashSet 和测试列表的初始化。请看一下
  • 不,但您可能正在做某事。它现在在我的适配器中,在我的位置activity 中显示原始“位置”或“默认位置”array/arraylist。所以本质上你是对的,我的列表是空的,因为我在 set 方法中所做的任何事情都不会在我的 get 方法中返回
  • 我很抱歉,但我不明白一般的想法。据我了解,您正试图在 ListView? 中显示一些具有相应频率的值?方法setPlacesSmoked 没有在任何地方被调用(应该修改HashSet,对吧?)
  • this post 是最初导致我找到这个解决方案的原因。也许阅读它会帮助你理解。很抱歉让您感到困惑,感谢您的帮助
猜你喜欢
  • 2013-03-10
  • 1970-01-01
  • 2018-07-12
  • 2017-08-15
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
相关资源
最近更新 更多