如何将数据库里的数据添加到listview中

如何将数据库里的数据添加到listview中,第1张

有两种办法 一种是自动绑定 还有一种手动绑定

Private Sub Form_Load()

On Error GoTo ErrProceed

Dim ObjConnTmp As New ADODB.Connection, DataSource As String

DataSource = "数据库名"

ObjConnTmp.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" &DataSource &""

ObjConnTmp.Open

Dim ObjTmpRs As New ADODB.Recordset

Set ObjTmpRs.ActiveConnection = ObjConnTmp

ObjTmpRs.CursorType = adOpenDynamic

ObjTmpRs.LockType = adLockOptimistic '设置游标类型

StrTmpSQL = "Select * from 数据库表名"

ObjTmpRs.Open StrTmpSQL

FrmMain.ListView1.View = lvwReport

FrmMain.ListView1.ColumnHeaders.Clear

Call ModListView.SubListViewHeader(FrmMain.ListView1, ObjRsTmp) '加标题

FrmMain.ListView1.ListItems.Clear

Call ModListView.SubListViewListItem(FrmMain.ListView1, ObjRsTmp, "End") '加内容

ObjRsTmp.Close: Set ObjRsTmp = Nothing

ObjConnTmp.Close: Set ObjConnTmp = Nothing

Exit Sub

ErrProceed:

MsgBox "程序发生错误,原因如下:" &vbCrLf &Err.Description

End Sub

Public Sub SubListViewHeader(ByVal ListViewName As ListView, ByVal ObjRs As ADODB.Recordset)

'功能:将相应的记录的 标题加到ListView中去

Dim clmX As ColumnHeader

'设置显示方式

'ListViewName.View = lvwReport

'要想显示出网格,请在属性/通用/网格行 设置

nCounter = ObjRs.Fields.Count - 1

' Dim nWidthGroup() As Integer

' ReDim Preserve nWidthGroup(nCounter)

Dim nWidthGroup(8) As Integer

k = 100

nWidthGroup(0) = k * 6 '[编号]

nWidthGroup(1) = k * 30 '[名称]

nWidthGroup(2) = k * 11 '[大小]

nWidthGroup(3) = k * 8 '[类型]

nWidthGroup(4) = k * 20 '[修改日期]

nWidthGroup(5) = k * 20 '[记录日期]

nWidthGroup(6) = k * 0 '[文件]

For i = 0 To nCounter

nWidth = nWidthGroup(i)

Set clmX = ListViewName.ColumnHeaders.Add(, , ObjRs.Fields(i).Name, nWidth)

Select Case ObjRs.Fields(i).Name

Case "类型"

clmX.Alignment = lvwColumnCenter

Case "大小"

clmX.Alignment = lvwColumnRight

End Select

Next

End Sub

Public Sub SubListViewListItem(ByVal ListViewName As ListView, ByVal ObjRs As ADODB.Recordset, ByVal StrLocate As String)

'功能:将相应的记录的 内容加到ListView中去

'StrLocate 用来定位新加记录在原ListView中的相对位置 Begin,开头 End 结尾

Dim itmX As ListItem

'设置显示方式

'ListViewName.View = lvwReport

'要想显示出网格,请在属性/通用/网格行 设置

'加内容

If ObjRs.EOF And ObjRs.BOF Then Exit Sub

ObjRs.MoveFirst

Do Until ObjRs.EOF

If StrLocate = "End" Then

Set itmX = ListViewName.ListItems.Add(, , ObjRs(0))

Else

Set itmX = ListViewName.ListItems.Add(1, , ObjRs(0))

End If

'向列表中加入数据

For i = 1 To ObjRs.Fields.Count - 1

StrSubItem = FunNullConvert(ObjRs(i))

Select Case ObjRs.Fields(i).Name

Case "大小" '如果是文件大小,则格式化其显示方式为加上千分位

StrSubItem = Format(CLng(StrSubItem), "#,###")

Case "修改日期", "记录日期"

If StrSubItem <>"" Then StrSubItem = FormatDateTime(CDate(StrSubItem), vbGeneralDate)

End Select

itmX.SubItems(i) = CStr(StrSubItem)

Next i

ObjRs.MoveNext

Loop

End Sub

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)

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)

}


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

原文地址: http://outofmemory.cn/sjk/9960260.html

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

发表评论

登录后才能评论

评论列表(0条)

保存