DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: melqui
今日帖子: 23
在线用户: 15
导航: 论坛 -> 移动应用开发 斑竹:flyers,iamdream  
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/13 17:52:46
标题:
关于DELPHI FireMonkey ListView小问题请教 浏览:1263
加入我的收藏
楼主: 各位大佬们,我又来叨扰大家了,有两个小问题想请教一下:

1.如何定位ListView选中行的数据集问题
我现在采用的方式是CDS1.RecNo:=listview1.Selected.Index+1;
但这种方法有一个毛病,如果ListView打开的搜索框,这个定位就不准

2.如下图:ListView左滑删除的功能到底应该调用什么事件呢,我现在写得代码如下:会报错,错误信息:At begining of Table

procedure TFrmMain.ListView1DeleteItem(Sender: TObject; AIndex: Integer);
begin
   CDS1.RecNo:=ListView1.ItemIndex+1;
   UID:=CDS1.FieldByName('UID').AsString;
   if CDS1.FieldByName('Deployer').AsString = FrmMain.Label1.Text then
   begin
      cds1.Close;
      CDS1.CommandText:='delete from WorkTask where UID = '''+UID+''' ';
      CDS1.Execute;
      CDS1.ApplyUpdates(0);
   end else
   begin
     ShowMessage('你不是创建人,不能删除此项目');
     Exit;
   end;
end;
此帖子包含附件:
JPEG 图像
大小:471.5K
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/13 18:57:21
1楼: 搞定了一个问题,关于左滑删除调错了事件
正确的事件:ListView1DeletingItem
而我调用的是:ListView1DeleteItem

但ListView定位数据集的问题,还是没想到什么好办法,还请大家指点
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/13 21:29:36
2楼: using LiveBinding the reposition is automatic.

or 

you can try this manually:

implementation

var
  MyItemClicked  : TListViewItem = nil;

...

procedure TForm1.ListView1ItemClick(const Sender: TObject; const AItem: TListViewItem);
begin
  MyItemClicked := AItem; // the current item...
end;

procedure TForm1.ListView1DeletingItem(Sender: TObject; AIndex: Integer; var ACanDelete: Boolean);
begin
  // before delete item ... Delete or not?
end;

procedure TForm1.ListView1DeleteItem(Sender: TObject; AIndex: Integer);
begin
  Memo1.Lines.Add('Occurs when a user requests the deletion of a list view item, after this deletion takes place.: ' + AIndex.ToString);
end;

procedure TForm1.ListView1ButtonClick(const Sender: TObject; const AItem: TListItem; const AObject: TListItemSimpleControl);
var
  MyNextItemIndex :integer;
begin
  if (MyItemClicked = nil) then
    exit;
  //
  // if ((MyItemClicked.Index + 1)<= (ListView1.Items.Count - 1)) then
  ListView1.ItemIndex := MyItemClicked.Index + 1;
  //
  //ListView1.EditMode := false;
  //
  MyItemClicked := nil;
  //
  ShowMessage('this item was deleted...');
end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/13 21:58:55
3楼: CDS1.RecNo:=ListView1.ItemIndex+1;

--->  Recno != ItemIndex

ex.: 
Recno = 1.000.000 (record)
ItemIndex = 10 (on listview)
----------

if you need store the "Recno" value try this:

var
  LVI: TListViewItem;
begin
  EmployeeTable.Open;
  //
  while not EmployeeTable.Eof do
    begin
      LVI      := ListView1.Items.Add;
      LVI.Text := EmployeeTable.Fields[1].AsString;
      LVI.Tag  := EmployeeTable.RecNo;  // store the real recno value in "Tag" or any other property "integer"
      EmployeeTable.Next;
    end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/14 20:57:43
4楼: 大哥,你写得这些我表示看不懂啊,我的问题是,想获取ListView选中行对应的ClientDataSet的记录行,从而获得ClientDataSet这一行的数据

VCL里面,如果是DBGrid,那么数据集记录会跟着Grid的移动而改变,我们只需要写一句:FDQuery1.Fieldbyname('XXX').asstring,就能获得数据

但使用ListView就不行了,无论你点哪一行,数据集并不会跟着动,所以无法定位
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/14 22:02:14
5楼: ListView is not "aware-data", then, you needs use some way to store the values... for example, using "TAG/TAGString" properties!!!

ListView1.ItemIndex = "n" item in a total of items on ListView (Count)!!!
Recno = current record on the table!!!

you see? this values is "distinct" each other!!

for example:
----------
using "TAG" property in listview items, you can store the "RECNO" value, then, you use it like a "bookmark" (fake).
...
  // all ListView "Items" have:  ... ListView1.ItemXXX.Tag := ID value in table -- as above showed

  if DataSetXXXX.Locate('ID', ListViewItemXXXX.Tag, []) then
    // what to do?
      ;



---------- LiveBinding use: it's more easy!

if you are using "LiveBinding" you can use: just this line!!!

procedure TForm1.ListView1ItemClick(const Sender: TObject; const AItem: TListViewItem);
begin
  ShowMessage(BindSourceDB1.DataSet.FieldByName('FIRST_NAME').AsString);
end;
此帖子包含附件:
PNG 图像
大小:22.4K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/15 1:14:10
6楼: 我是使用的"LiveBinding",但不能实现你说的功能

Label2.Text:=BindSourceDB1.DataSet.FieldByName('ALine').AsString;

这句代码我在Label2的Text属性上永远都只能获得第一行的数据,不知道是否与我使用的ListView设置有关,我的设置如下图所示:
此帖子包含附件:
PNG 图像
大小:39.4K
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/15 1:36:15
7楼: 另外还想请问,在上面的这个图里面,我如何获取ListView中Item Text1的值,因为这种方式完全与之前电脑端VCL所用的不一样了
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/15 5:07:49
8楼: "BindSourceDB" works like a "DataSource" in common datasets!!!

if ListView go to another "item", then, using LiveBinding, BindSourceDB you go to another record ...  relationship: 1 to 1
----------

Using ListView in DinamicAppearance, you can get the data from Items that way:

if (ListView1.ItemIndex > -1) then
ShowMessage(ListView1.Items[ListView1.ItemIndex].Data['Text2'].AsString);

"Text2" is the name from item on DinamicAppearance style.

NOTE: "Text" does not works in DynamicAppearance, because it is used for another style, like "ListItem", for example.

My tip:
--- Change your Dataset to FireDAC (FDQuery "for database" or FDMemTable "for memory usage") is better and works with all data sources, like: Databases, ODBC, XML, JSON, include text files!!!
--- using new technologies like FireDAC your app have a long-life
此帖子包含附件:
PNG 图像
大小:53.8K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/15 8:36:43
9楼: To:emailx45

非常感谢,是的,按照您的帮助我已成功获得想要的数据,而且解决了搜索状态下定位错误的问题,再次感谢您的帮助
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/15 9:47:31
10楼: not for that.

hug
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 ywqq (ywq) ★☆☆☆☆ -
盒子活跃会员
2022/10/15 16:27:56
11楼: 非常感谢,学习了
----------------------------------------------
-
作者:
男 ywqq (ywq) ★☆☆☆☆ -
盒子活跃会员
2022/10/15 16:51:18
12楼: To: changfenglee (李向阳)
获取ListView选中行对应的ClientDataSet的记录行,从而获得ClientDataSet这一行的数据, 你是怎样做到的?
Label2.Text:=BindSourceDB1.DataSet.FieldByName('ALine').AsString;
这句代码我在Label2的Text属性上永远都只能获得第一行的数据
(ListView1.Items[ListView1.ItemIndex].Data['Text2'].AsString
可以找到显来出来的数据,
没有显出来的字段,怎样找到对应的数据?
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/10/15 21:05:51
13楼:   Memo1.Lines.Clear;
  //
  for var MyItem in ListView1.ItemAppearanceObjects.ItemObjects.Objects do
    Memo1.Lines.Add(
      ListView1.Items[ListView1.ItemIndex].Data[ MyItem.Name ].AsString
    );

NOTE: if "MyItem.Name = ''", dont worry!!! None error, just "empty" value will be showed.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/17 11:20:52
14楼: TO:ywqq

您可以参考emailx45写的代码,这样是可以准确定位到对应记录的,同时你也可以做一下变通,比如在Bind数据的时候,将数据表的唯一字段也进行绑定,只是设置ListView对应的Text不显示就好,比如UID,RID之类的,那后续就可以根据这个值进行精准操作
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
作者:
男 changfenglee (葫芦老四) ▲▲▲▲▲ -
普通会员
2022/10/17 11:25:29
15楼: To:emailx45

还有个问题想请教您,内容如下:

我现在ListView数据绑定,基本都是静态绑定的模式,但发现有时候静态绑定不够灵活,我想实现动态绑定模式,但试了几种方法,都不成功,可能对此的理解度是不够的,您能指点一下吗

比如:ItemAppearance模式下的Text1如何绑定BindSourceDB1上面的ALine字段?
----------------------------------------------
【个人签名】:玩了多年DELPHI,终于从菜鸟升级成老菜鸟
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行117.1875毫秒 RSS