如何向listview中动态添加数据

如何向listview中动态添加数据,第1张

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)

}

package com.example.listview

2

3 import android.content.Context

4 import android.util.AttributeSet

5 import android.view.LayoutInflater

6 import android.view.View

7 import android.widget.ListView

8 import android.widget.ProgressBar

9 import android.widget.TextView

10

11 /**

12 * Created by keranbin on 2015/10/25.

13 */

14 public class ZdyListView extends ListView {

15 private Context context

16 private View footer

17 private ProgressBar progressBar

18 private TextView tv

19

20 public ZdyListView(Context context) {

21 super(context)

22 init(context)

23 }

24 public ZdyListView(Context context, AttributeSet attrs) {

25 super(context, attrs)

26 init(context)

27 }

28 public ZdyListView(Context context, AttributeSet attrs, int defStyle) {

29 super(context, attrs, defStyle)

30 init(context)

31 }

32 private void init(Context context) {

33 this.context=context

34 footer=LayoutInflater.from(context).inflate(R.layout.activity_footer, null)

35 this.addFooterView(footer)

36 progressBar=(ProgressBar) footer.findViewById(R.id.progressBar)

37 tv=(TextView) footer.findViewById(R.id.tv)

38 tv.setText("上拉加载更多")

39 }

40

41 //正在加载数据,将listview底部提示文字置为"正在加载中。。。。"

42 public void onLoading(){

43 progressBar.setVisibility(VISIBLE)

44 tv.setText("正在加载中。。。。")

45 }

46

47 //加载完毕,将listView底部提示文字改为"上拉加载更多"

48 public void LoadingComplete(){

49 progressBar.setVisibility(GONE)

50 tv.setText("上拉加载更多")

51 }

52

53

54 //重写onMeasure,解决scrollview与listview冲突

55 @Override

56 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

57 int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2,

58 MeasureSpec.AT_MOST)

59 super.onMeasure(widthMeasureSpec, expandSpec)

60 }

61

62


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存