Delphi Listview如何实现Shift-Up和Shift-Down快捷键功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计463个文字,预计阅读时间需要2分钟。
在ListView控件中,通常具备一项功能,允许上下移动项目。关于您提到的TListView(主要使用数据库网格),以下是一种可能的实现方式:
java// 假设有一个数据库适配器dbAdapter用于获取数据Cursor cursor=dbAdapter.queryDatabase(SELECT * FROM your_table);
// 创建一个SimpleCursorAdapterSimpleCursorAdapter adapter=new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, cursor, new String[] { column_name }, new int[] { android.R.id.text1 });
// 设置ListView的适配器listView.setAdapter(adapter);
// 实现上下移动项目listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { // 获取要移动的项目数据 Cursor cursor=(Cursor) listView.getItemAtPosition(position); String itemData=cursor.getString(cursor.getColumnIndex(column_name));
// 移动项目逻辑 // ...
// 更新数据库和ListView adapter.notifyDataSetChanged(); return true; }});
以上代码仅为示例,具体实现可能需要根据您的具体需求进行调整。希望这能对您有所帮助。
Listview控件中是否有一项功能可以上下移动项目? 我没有很好地使用TListView(我主要使用数据库网格),我把你的问题作为学习的机会.以下代码是结果,大卫的答案更具视觉导向性.它有一些限制:它只会移动第一个选定的项目,当它移动项目时,vsicon和vsSmallIcon的显示在移动后很奇怪.procedure TForm1.btnDownClick(Sender: TObject); var Index: integer; temp : TListItem; begin // use a button that cannot get focus, such as TSpeedButton if ListView1.Focused then if ListView1.SelCount>0 then begin Index := ListView1.Selected.Index; if Index<ListView1.Items.Count then begin temp := ListView1.Items.Insert(Index+2); temp.Assign(ListView1.Items.Item[Index]); ListView1.Items.Delete(Index); // fix display so moved item is selected/focused ListView1.Selected := temp; ListView1.ItemFocused := temp; end; end; end; procedure TForm1.btnUpClick(Sender: TObject); var Index: integer; temp : TListItem; begin // use a button that cannot get focus, such as TSpeedButton if ListView1.Focused then if ListView1.SelCount>0 then begin Index := ListView1.Selected.Index; if Index>0 then begin temp := ListView1.Items.Insert(Index-1); temp.Assign(ListView1.Items.Item[Index+1]); ListView1.Items.Delete(Index+1); // fix display so moved item is selected/focused ListView1.Selected := temp; ListView1.ItemFocused := temp; end; end; end;
本文共计463个文字,预计阅读时间需要2分钟。
在ListView控件中,通常具备一项功能,允许上下移动项目。关于您提到的TListView(主要使用数据库网格),以下是一种可能的实现方式:
java// 假设有一个数据库适配器dbAdapter用于获取数据Cursor cursor=dbAdapter.queryDatabase(SELECT * FROM your_table);
// 创建一个SimpleCursorAdapterSimpleCursorAdapter adapter=new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, cursor, new String[] { column_name }, new int[] { android.R.id.text1 });
// 设置ListView的适配器listView.setAdapter(adapter);
// 实现上下移动项目listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { // 获取要移动的项目数据 Cursor cursor=(Cursor) listView.getItemAtPosition(position); String itemData=cursor.getString(cursor.getColumnIndex(column_name));
// 移动项目逻辑 // ...
// 更新数据库和ListView adapter.notifyDataSetChanged(); return true; }});
以上代码仅为示例,具体实现可能需要根据您的具体需求进行调整。希望这能对您有所帮助。
Listview控件中是否有一项功能可以上下移动项目? 我没有很好地使用TListView(我主要使用数据库网格),我把你的问题作为学习的机会.以下代码是结果,大卫的答案更具视觉导向性.它有一些限制:它只会移动第一个选定的项目,当它移动项目时,vsicon和vsSmallIcon的显示在移动后很奇怪.procedure TForm1.btnDownClick(Sender: TObject); var Index: integer; temp : TListItem; begin // use a button that cannot get focus, such as TSpeedButton if ListView1.Focused then if ListView1.SelCount>0 then begin Index := ListView1.Selected.Index; if Index<ListView1.Items.Count then begin temp := ListView1.Items.Insert(Index+2); temp.Assign(ListView1.Items.Item[Index]); ListView1.Items.Delete(Index); // fix display so moved item is selected/focused ListView1.Selected := temp; ListView1.ItemFocused := temp; end; end; end; procedure TForm1.btnUpClick(Sender: TObject); var Index: integer; temp : TListItem; begin // use a button that cannot get focus, such as TSpeedButton if ListView1.Focused then if ListView1.SelCount>0 then begin Index := ListView1.Selected.Index; if Index>0 then begin temp := ListView1.Items.Insert(Index-1); temp.Assign(ListView1.Items.Item[Index+1]); ListView1.Items.Delete(Index+1); // fix display so moved item is selected/focused ListView1.Selected := temp; ListView1.ItemFocused := temp; end; end; end;

