是否可以在java中将ArrayList<SelectItem>转换为ArrayList<Long>
我的清单如下:
List<SelectItem> measurementList;
我想将measurementList.get(i)
赋值给Long
变量
例如,我希望通过以下方式分配它:
Long sd = measurementList.get(0);
但是,它显示出类型不匹配。这是很明显的。如何将measurementList.get(0)
中的值赋值给Long
变量
谢谢
代码如下:
public List<SelectItem> measurementList(Long coModAssetId)
{
// log.info("Start");
// log.debug("finding MeasurementSeneorTypeList instance by example using dbMeasurementSeneorTypeList");
List<SelectItem> results;
try {
// Add SELECT with a nested select to get the 1st row
String queryString = "SELECT M.Measurement_Id, M.NAME" +
" FROM ems.COMPANY_MODULE_ASSET CMA, " +
" ems.Asset_Measurement Am," +
" ems.MEASUREMENT M" +
" WHERE CMA.Co_Mod_Asset_Id = ?" +
" And Cma.Asset_Id = Am.Asset_Id " +
" AND AM.MEASUREMENT_ID = M.MEASUREMENT_ID" +
" GROUP BY M.measurement_id, M.NAME" +
" ORDER BY M.name";
MeasurementSeneorTypeListWork work = new MeasurementSeneorTypeListWork();
work.coModAssetId = coModAssetId;
work.queryString = queryString;
getSession().doWork(work);
results = work.results;
} catch (RuntimeException re) {
// log.error("getMostRecentObservationId() failed", re);
throw re;
}
//log.info("End");
return results;
}
类定义
public class MeasurementSeneorTypeListWork implements Work {
List<SelectItem> results = new ArrayList<SelectItem>();
private String queryString;
private Long coModAssetId;
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(queryString);
int index = 1;
ps.setLong(index++, coModAssetId);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
//String userName = PropertyReader.getLabel(rs.getString(2));
Long id = rs.getLong(1);
SelectItem item = new SelectItem(id, null);
results.add(item);
}
rs.close();
ps.close();
}
}
}
# 1 楼答案
您可以使用
.longValue()
方法:这将返回转换为long类型后此对象表示的数值
# 2 楼答案
如果
SelectItem
类型为javax.faces.model.SelectItem
,则可以使用getValue()
方法注意:您正在使用2参数构造函数
SelectItem(Object value, String label)
为列表创建项,但是由于第二个参数始终是null
,因此可以使用SelectItem(Object value)