【发布时间】:2016-04-28 04:08:32
【问题描述】:
我从 mysql 数据库中获取值。这是我的代码:
locList1 = new ArrayList<Locations_Master>();
locList1 = locDAO.getLM(); //contains the list of all Cities and Barangays from database
cityS1 = "";
citySL1 = new LinkedHashSet<String>();
for(int i = 0; i<locList1.size(); i++){
cityS1 = locList1.get(i).getCity() + "\n";
citySL1.add(cityS1);
}
cityList1 = new JComboBox(citySL1.toArray());
cityList1.setBounds(5, 110, 170, 20);
mainPanel.add(cityList1);
cityList1.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
selectedCity = cityList1.getSelectedItem().toString();
mainPanel.validate();
mainPanel.repaint();
}
});
barSL1 = new LinkedHashSet<String>();
for(int i=0; i<locList1.size(); i++){
if(locList1.get(i).getCity().equals(selectedCity)){
barS1 = locList1.get(i).getBarangay()+"\n";
barSL1.add(barS1);
//I tried filtering the barangay values here dependent on the selected city
}
System.out.println(barSL1);
}
barList1 = new JComboBox(barSL1.toArray());
barList1.setBounds(185, 110, 170, 20);
mainPanel.add(barList1);
据说,每次用户选择一个城市时,都会显示该城市下列出的描笼涯。输出中没有“错误”,但 barSL1 是 [],如输出所示。
【问题讨论】: