我的微调器只加载List中的第一个数据(字符串)

时间:2017-11-08 18:58:36

标签: java android eclipse

Courseprof.java:

这是迭代我的数据并将其放到微调器上,但是当我运行应用程序时,它只加载列表中的第一个数据。例如:我的列表中有一些字符串是[" r12 cs100 a20"," r02 cs120 a40"," r90 cs001 a30"。只有" r12 cs100 a20"]被加载到我的微调器中。但我想加载列表中的所有数据:'(

@SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<Properties> courseusers) {
        for (Iterator i = courseusers.iterator(); i.hasNext();) {
            Properties p = (Properties) i.next();

            Spinner mySpinner = (Spinner) findViewById(R.id.spincourse);

             List<String> itemList =  Arrays.asList(p.getCourseId()+" "+p.getRoom()+" "+p.getSection());
             ArrayAdapter<String> adapter;
              adapter = new ArrayAdapter(this, 
                     android.R.layout.simple_spinner_item,
                     itemList);

             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

             mySpinner.setAdapter(adapter);


        }
    }

Properties.java:

String roomid,courseid,sectionid;
    int profid;
    public String getRoom() {
        return roomid;
    }
    public void setRoom(String roomid) {
        this.roomid = roomid;
    }

    public String getCourseId() {
        return courseid;
    }
    public void setCourse(String courseid) {
        this.courseid = courseid;
    }
    public String getSection() {
        return sectionid;
    }
    public void setSection(String sectionid) {
        this.sectionid = sectionid;
    }

1 个答案:

答案 0 :(得分:2)

因为您在循环的每个迭代中都使用list元素创建single以及适配器和微调器初始化。

解决方案:将微调器和适配器代码移到循环外(最后)并使用可变列表在循环中添加数据。

// move initialization of references before loop
Spinner mySpinner = (Spinner) findViewById(R.id.spincourse);
List<String> itemList =  new ArrayList<>();

for (Iterator i = courseusers.iterator(); i.hasNext();) {
    Properties p = (Properties) i.next();
    // add items to list
    itemList.add(p.getCourseId()+" "+p.getRoom()+" "+p.getSection());     
}
// data list is ready so setup adapter and display spinner
ArrayAdapter<String> adapter;
  adapter = new ArrayAdapter(this, 
         android.R.layout.simple_spinner_item,
         itemList);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

mySpinner.setAdapter(adapter);