tkintwr中listview如何添加数据

tkintwr中listview如何添加数据,第1张

ListView控件数据 *** 作一、添加表头和数据

在窗口显示数据时,经常会用到ListView这个控件,虽然也可以使用ListBox,但其功能还是比较弱一些,ListView相对用起来更顺手一些。下面的两个函数是给控件添加表头和数据的函数,贴出来供大家参考。

函数代码给ListView添加数据,数据可以为数组或单行数据(逗号分隔)

Public Sub AddListViewData(ListViewName As Object, DateArr, Optional Header As Integer = 0, Optional AddData As Boolean = False)

'添加ListView数据,正常为数组,支持单行数据添加(逗号分隔)

'第一行数据为标题行时,Header应为1

'默认为替换数据,如果需要在原有数据基础上添加时,AddData应为True

Dim i As Integer, j As Integer

Dim DateCol() As String

Dim Itm As Object

Dim PartKey As String

If Not AddData Then ListViewName.ListItems.Clear

If IsArray(DateArr) Then

For i = LBound(DateArr) + Header To UBound(DateArr)

Set Itm = ListViewName.ListItems.Add()

Itm.Text = DateArr(i, LBound(DateArr, 2))

For j = LBound(DateArr, 2) + LBound(DateArr) To UBound(DateArr, 2)

Itm.SubItems(j - LBound(DateArr)) = DateArr(i, j)

Next

Next

Else

If IsEmpty(DateArr) Or DateArr = "没有记录" Then Exit Sub

DateCol = Split(DateArr, ",")

Set Itm = ListViewName.ListItems.Add()

Itm.Text = DateCol(LBound(DateCol))

For i = LBound(DateCol) + LBound(DateArr) To UBound(DateCol)

Itm.SubItems(i - LBound(DateArr)) = DateCol(i)

Next

End If

End Sub

给ListView添加表头,支持直接给定宽度和额外增加的宽度

Public Sub AddListViewHead(ListViewName As Object, ColHeader, Optional ColWidth As String, Optional AddWidth As Integer = 5, Optional DefultWidth As String = "Auto")

Dim SpHeader() As String

Dim SpWidth() As String

Dim SpAlign() As String

Dim CW As Integer

Dim CA As Integer

Dim i As Integer

ListViewName.ColumnHeaders.Clear

ListViewName.ListItems.Clear

SpWidth = Split(ColWidth, ",")

If UBound(SpWidth) = 0 Then CW = Val(ColWidth)

With ListViewName

If IsArray(ColHeader) Then

For i = LBound(ColHeader, 2) To UBound(ColHeader, 2)

If i <= UBound(SpWidth) Then CW = Val(SpWidth(i)) Else CW = IIf(DefultWidth = "Auto", 0, CW)

If CW >= 0 And CW <Len(StrConv(ColHeader(0, i), vbFromUnicode)) * 15 + AddWidth Then CW = Len(StrConv(ColHeader(0, i), vbFromUnicode)) * 15 + AddWidth

If CW <0 Then CW = 0

.ColumnHeaders.Add , , ColHeader(0, i), CW

Next

Else

If ColHeader = " *** 作不成功" Then Exit Sub

SpHeader = Split(ColHeader, ",")

For i = LBound(SpHeader) To UBound(SpHeader)

If i <= UBound(SpWidth) Then CW = Val(SpWidth(i)) Else CW = IIf(DefultWidth = "Auto", 0, CW)

If CW >= 0 And CW <LenB(StrConv(SpHeader(i), vbFromUnicode)) * 7.5 + AddWidth Then CW = LenB(StrConv(SpHeader(i), vbFromUnicode)) * 7.5 + AddWidth

If CW <0 Then CW = 0

.ColumnHeaders.Add , , SpHeader(i), CW

Next

End If

.View = lvwReport

.FullRowSelect = True

.Gridlines = True

End With

End Sub

调用例子

Sub FormLoad()

AddListViewHead 列表, Range("A1:D1").Value

tmp = SumArrRow(Range("a1:c4").Value, "1,3", True)

AddListViewData 列表, tmp

End Sub

在Qt中,可以使用addItem()函数来增加一行数据到listview中。例如,你可以使用以下语句在listview中添加一行数据:

listView->addItem("1","2","3","4")

ListView list = (ListView) findViewById(R.id.ListView01)

//生成动态数组,加入数据

ArrayList<HashMap<String, Object>>listItem = new ArrayList<HashMap<String, Object>>()

for(int i=0i<10i++)

{

HashMap<String, Object>map = new HashMap<String, Object>()

map.put("ItemImage", R.drawable.checked)//图像资源的ID

map.put("ItemTitle", "Level "+i)

map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ")

listItem.add(map)

}

//生成适配器的Item和动态数组对应的元素

SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源

R.layout.list_items,//ListItem的XML实现

//动态数组与ImageItem对应的子项

new String[] {"ItemImage","ItemTitle", "ItemText"},

//ImageItem的XML文件里面的一个ImageView,两个TextView ID

new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}

)

//添加并且显示

list.setAdapter(listItemAdapter)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11629036.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存