listview对应数据表,想根据自己的要求分类显示时发现在第二行开始显示缺少一个图标,非常奇怪
比如:数据如房间1101 1102 1103 1104最后显示如果此数据在第二行以后就不会显示1102直接变成了1103等于跳过了一个纪录,但如果查找此图标的caption却显示有此图标,以列表方式查看也有,只是大图标按我指定位置显示图标时没有.代码如下:
var
  i, Ix, Iy, J:integer;
  listitem:tlistitem;
begin
  Listview1.Items.Clear;
  With DSt_Room do
  begin
    Ix:=0;
    Iy:=0;
    for i:=1 to RecordCount do
    begin
      ListItem:=Listview1.Items.Add;
      ListItem.Caption :=FieldByName('RoomNo').AsString;
      listitem.SubItems.add(FieldByName('Floor').AsString);
      listitem.ImageIndex:=FieldByName('RoomStateID').Value;      if J=FieldByName('floor').Value then
      begin
        if Ix<Listview1.Width-64 then
        begin
          ListItem.Left:=Ix;
          ListItem.Top:=Iy;
          Ix:=Ix+65;
        end
        else
        begin
          Ix:=0;
          IY:=iy+60;
          ListItem.Left:=Ix;
          ListItem.Top:=Iy;
        end;
      end
      else
      begin
        Ix:=0;
        IY:=iy+80;
        ListItem.Left:=Ix;
        ListItem.Top:=Iy;
        J:=FieldByName('floor').Value;
      end;
      next;
    end;
  end;
end;

解决方案 »

  1.   

    代码中有几处可能有问题的地方,注释出来了:
    var
      i, Ix, Iy, J:integer;
      listitem:tlistitem;
    begin
      Listview1.Items.Clear;
      With DSt_Room do
      begin
        Ix:=0;
        Iy:=0;
        for i:=1 to RecordCount do
        // 遍历DataSet最好还是用While not DS.Eof do 和 DS.Next,用RecordCount对于大数据量效率很低
        begin
          ListItem:=Listview1.Items.Add;
          ListItem.Caption :=FieldByName('RoomNo').AsString;
          listitem.SubItems.add(FieldByName('Floor').AsString);
          listitem.ImageIndex:=FieldByName('RoomStateID').Value;      if J=FieldByName('floor').Value then
          // 变量J没有初始值,这条语句的执行存在不确定性
          begin
            if Ix<Listview1.Width-64 then
            begin
              ListItem.Left:=Ix;
              ListItem.Top:=Iy;
              Ix:=Ix+65;
            end
            else begin
              Ix:=0;
              IY:=iy+60;
              ListItem.Left:=Ix;
              ListItem.Top:=Iy;
            end;
          end
          else begin
            Ix:=0;
            IY:=iy+80;
            ListItem.Left:=Ix;
            ListItem.Top:=Iy;
            J:=FieldByName('floor').Value;
            // 你这里读入J干什么,马上就要执行Next了
            // 读出来的J是前一条记录的Floor,然后你去用来判断是否在当前Floor显示新纪录??!!
          end;
          next;
        end;
      end;
    end;
      

  2.   

    不好意思,复制代码的时个是少了一条对J的最初赋值语句在
     Ix:=0;
     Iy:=0;
     J:=FieldByName('Floor').Value;//这是我忘了复制上,现在关键是不是我遍历数据集的时候出的问题,是加item的时候如果用列表方式显示,是不会缺少items的,但只要以大图标显示就会从第二行起在第二个显示的图标中少一个数据文件图标,如上边所讲,但如果我不自己定义图标的位置,则完全正常,也就是把定义图标位置的语句注释掉,以默认位置加item则又是正常的,为什么呢,郁闷中!
      

  3.   

    问题已解决,还是自己代码问题,正确代码如下:
    var
      i, Ix, Iy, J:integer;
      listitem:tlistitem;
    begin
      Listview1.Items.Clear;
      With DSt_Room do
      begin
      Ix:=0;
      Iy:=0;
      J:=FieldByName('Floor').Value;
      for i:=1 to RecordCount do
      begin
        ListItem:=Listview1.Items.Add;
        ListItem.Caption :=FieldByName('RoomNo').AsString;
        listitem.SubItems.add(FieldByName('Floor').AsString);
        listitem.ImageIndex:=FieldByName('RoomStateID').Value;    if J=FieldByName('floor').Value then
        begin
          if Ix<Listview1.Width-64 then
          begin
            ListItem.Left:=Ix;
            ListItem.Top:=Iy;
            Ix:=Ix+65;
          end
          else
          begin
            Ix:=0;
            IY:=iy+60;
            ListItem.Left:=Ix;
            ListItem.Top:=Iy;
            Ix:=Ix+65; //此处新加,少了此条语句,结果是第二行起的第二个图标与第二行第一个图标完全重合
          end;
        end
        else
        begin
          Ix:=0;
          IY:=iy+80;
          ListItem.Left:=Ix;
          ListItem.Top:=Iy;
          J:=FieldByName('floor').Value;
          Ix:=Ix+65; //此处新加    
        end;
        next;
      end;
    end;
      

  4.   

    标记一下,学习了。
    遍历DataSet最好还是用While not DS.Eof do 和 DS.Next,用RecordCount对于大数据量效率很低。
    楼主还是改为while语句试一下。