展会信息港展会大全

通过ListActivity使用ListView布局方法
来源:互联网   发布日期:2016-01-14 10:10:02   浏览:2251次  

导读:先简单的介绍一下ListActivityListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显......

先简单的介绍一下ListActivity

ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

我们知道

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

下面的代码都直接继承了ListActivity,采用simpleAdapter

先上两个图:

上代码

MoreActivity.java

01

import java.util.ArrayList;

02

import java.util.HashMap;

03

import java.util.List;

04

import java.util.Map;

05

import android.app.AlertDialog;

06

import android.app.ListActivity;

07

import android.content.DialogInterface;

08

import android.os.Bundle;

09

import android.view.View;

10

import android.widget.ListView;

11

import android.widget.SimpleAdapter;

12

13

public class MoreActivity extends ListActivity {

14

15

private String discount_info;

16

private String recommend_shop;

17

private String about_us;

18

19

@Override

20

public void onCreate(Bundle savedInstanceState) {

21

super.onCreate(savedInstanceState);

22

23

SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.more,

24

new String[]{"title","info","img"},

25

new int[]{R.id.title,R.id.info,R.id.img});

26

setListAdapter(adapter);

27

28

}

29

30

private List<HashMap> getData() {

31

List<HashMap> list = new ArrayList<HashMap>();

32

33

Map map = new HashMap();

34

35

discount_info=this.getString(R.string.discount_info);//相应字符串在String.xml中设置

36

recommend_shop=this.getString(R.string.recommend_shop);

37

about_us=this.getString(R.string.about_us);

38

39

map.put("title", discount_info);

40

map.put("info", "android 1");

41

map.put("img", R.drawable.i1);

42

list.add(map);

43

44

map = new HashMap();

45

map.put("title", recommend_shop);

46

map.put("info", "android 2");

47

map.put("img", R.drawable.i2);

48

list.add(map);

49

50

map = new HashMap();

51

map.put("title", about_us);

52

map.put("info", "android 3");

53

map.put("img", R.drawable.i3);

54

list.add(map);

55

56

return list;

57

}

58

59

@Override

60

protected void onListItemClick(ListView l, View v, int position, long id) {

61

// TODO Auto-generated method stub

62

super.onListItemClick(l, v, position, id);

63

if(0==id)

64

{

65

showInfo();//点击第一项执行函数,其它id可以自己设置执行函数

66

}

67

68

}

69

70

//定义启动Dialog

71

72

public void showInfo(){

73

new AlertDialog.Builder(this)

74

.setTitle("title")

75

.setMessage("bla..bla")

76

.setPositiveButton("OK", new DialogInterface.OnClickListener() {

77

@Override

78

public void onClick(DialogInterface dialog, int which) {

79

}

80

})

81

.show();

82

83

}

84

}

more.xml

01

<?xml version= 1.0encoding= utf-8 ?>

02

<LinearLayout xmlns:android= http://schemas.android.com/apk/res/android

03

android:orientation= horizontal

04

android:layout_width= fill_parent

05

android:layout_height= fill_parent >

06

<ImageView

07

android:id= @+id/img

08

android:layout_width= wrap_content

09

android:layout_height= wrap_content

10

android:layout_margin= 5px />

11

12

<LinearLayout

13

android:orientation= vertical

14

android:layout_width= wrap_content

15

android:layout_height= wrap_content >

16

17

<TextView

18

android:id= @+id/title

19

android:layout_width= wrap_content

20

android:layout_height= wrap_content

21

android:textColor= #000000

22

android:textSize= 22px/>

23

<TextView

24

android:id= @+id/info

25

android:layout_width= wrap_content

26

android:layout_height= wrap_content

27

android:textColor= #000000

28

android:textSize= 13px/>

29

30

</LinearLayout>

31

</LinearLayout>

赞助本站

人工智能实验室

相关热词: ListActivity ListView 布局

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港