ArrayList 问题 100
public void selectName(String n)throws
ClassNotFoundException{
try{
SQL= "select assetid,asset_name,model,price,purchase_date,status from asset where asset_name= ' "+n+ " ' ";
dc=new DbConn();
con=dc.getCon();
st=con.createStatement();
ResultSet rs=st.executeQuery(SQL);
list=new ArrayList <TCardModel> ();
while(rs.next()){
tcm=new TCardModel(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getDouble(4),rs.getString(5),rs.getString(6));
list.add(tcm);
j++;
}
if(j==0){
JOptionPane.showMessageDialog(null, "相关数据不存在 ", "error! ",JOptionPane.INFORMATION_MESSAGE);
}
else{
iter=list.listIterator();
tcm=(TCardModel)iter.next();
//tcm=list.get(2);
TCard.label2.setText(tcm.getAssetid());
TCard.label4.setText(tcm.getAsset_name());
TCard.label6.setText(tcm.getModel());
TCard.label8.setText(tcm.getPrice());
TCard.label10.setText(tcm.getPurchase_date());
TCard.label12.setText(tcm.getStatus());
if(iter.hasNext())
{
System.out.println( "有多条记录 ");
//TCard.nextbtn.setEnabled(true);
//TCard.proviousbtn.setEnabled(true);
}
}
}
catch(
SQLException sqle){
System.err.println(sqle);
}
}
public void next(){
if(iter.hasNext())
{
tcm=(TCardModel)iter.next();
TCard.label2.setText(tcm.getAssetid());
TCard.label4.setText(tcm.getAsset_name());
TCard.label6.setText(tcm.getModel());
TCard.label8.setText(tcm.getPrice());
TCard.label10.setText(tcm.getPurchase_date());
TCard.label12.setText(tcm.getStatus());
}else{System.out.println( "最后纪录 ");}
}
public void provious(){
if(iter.hasPrevious()){
tcm=(TCardModel)iter.previous();
TCard.label2.setText(tcm.getAssetid());
TCard.label4.setText(tcm.getAsset_name());
TCard.label6.setText(tcm.getModel());
TCard.label8.setText(tcm.getPrice());
TCard.label10.setText(tcm.getPurchase_date());
TCard.label12.setText(tcm.getStatus());
}
}
现在有两个按键对应这两个方法 分别实现 list内部上下滚动的功能 现在用next()能正常实现功能 但是provious却出现了问题 调用一次pro方法的时候iter.nextIndex()能改变 但是输出的信息却不变 第二次点击才会改变 谁能告诉我解决的方法 点击一次就能正常达到我想要的目的
------解决方案--------------------看者好象没有问题啊
------解决方案--------------------交替调用时返回相同的元素
------解决方案--------------------ListIterator.previous()方法没有问题,而是你使用的问题.
在ListIterator中,由nextIndex变量来指示下一个next()要读取的元素位置,previousIndex来指示下一个previous()要读取的元素位置,nextIndex和previousIndex有关系:
nextIndex = previousIndex + 1
假设当前nextIndex = i, previousIndex = i - 1, 当你调用next()时,返回第i个元素,此时nextIndex = i + 1, previousIndex = i. 当你立即调用previous()时,很显然会返回第i个元素,然后nextIndex = i, previousIndex = i - 1;
所以就出现了你描述的问题,这个问题只出现在由next()转入previous()或previous()转入next()的时候.当连续调用previous()或next()的时候是没有问题的.