实现代码如下:
package com.app.test01
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import android.R.integer
import android.app.Activity
import android.os.Bundle
import android.view.ContextMenu
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ContextMenu.ContextMenuInfo
import android.widget.AbsListView
import android.widget.AbsListView.OnScrollListener
import android.widget.AdapterView
import android.widget.AdapterView.AdapterContextMenuInfo
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import com.app.adapter.MyWeixinJSON
import com.app.adapter.MyWeixinList
/**
* @author 402-9
*
*/
public class ListViewPage extends Activity {
private ListView lv
private BaseAdapter mJson
private JSONArray mData = new JSONArray()// JSON数据源
private View view_page_footer// 底部视图
private int num = 1// 加载数据计数
private int count = 50// 总数据
// private boolean flag
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState)
setContentView(R.layout.weixin)
lv = (ListView) findViewById(R.id.lv)
getJSONArray(mData)
mJson = new MyWeixinJSON(mData, this)
view_page_footer = LayoutInflater.from(this).inflate(
R.layout.view_page_footer, null)
lv.addFooterView(view_page_footer)// 添加底部视图
TextView text_page = (TextView) view_page_footer.findViewById(R.id.text_page)
text_page.setOnClickListener(new View.OnClickListener() {
// 点击按钮 追加数据 并通知适配器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v
tv.setText("正在加载中...")
getJSONArray(mData)
tv.setText("下一页")
mJson.notifyDataSetChanged()
}
})
lv.setAdapter(mJson)// 绑定适配器
}
/** 数据源JSONArray */
private void getJSONArray(JSONArray jArray) {
try {
for (int i = 1i <= 5i++) {
JSONObject jsonObject = new JSONObject()
jsonObject.put("title", "姓名" + num++)
jsonObject.put("time", "9月29日")
jsonObject.put("info", "我通过了你的好友验证请求,现在我们可以开始对话啦")
jsonObject.put("img", R.drawable.special_spring_head2)
jArray.put(jsonObject)
if (num == count) {
lv.removeFooterView(view_page_footer)
Toast.makeText(this, "没有更多数据了...", Toast.LENGTH_LONG)
.show()
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
其中,所添加的底部视图,只有一个供点击追加的按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/text_page"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下一页"
android:gravity="center"/>
</LinearLayout>
其中,所添加的底部视图,只有一个供点击追加的按钮:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/text_page"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下一页"
android:gravity="center"/>
</LinearLayout>
效果图
点击“下一页”,在ListView后追加数据。
追加完成后,清除底部视图。
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
listview表现的数据都是来源与你adapter的数据,把一个listview的数据加到另一个,就是把它的数据加到另一个listview的数据里,然后调用adapter的notifydatasetchange欢迎分享,转载请注明来源:内存溢出
评论列表(0条)