07_ListView

  ListView是用来显示一个列表的控件,它在Android源代码中是继承AbsListView类的,AbsListView类是继承于AdapterView类的,而AdapterView类是继承ViewGroup类的。

 

  下面程序的功能:创建ListView,点击Item会显示全部信息。

07_ListView

 1 package com.example.listviewdemo;

 2 

 3 import android.support.v7.app.ActionBarActivity;

 4 import android.os.Bundle;

 5 import android.view.View;

 6 import android.widget.AdapterView;

 7 import android.widget.AdapterView.OnItemClickListener;

 8 import android.widget.ArrayAdapter;

 9 import android.widget.ListView;

10 import android.widget.Toast;

11 

12 public class MainActivity extends ActionBarActivity implements OnItemClickListener {

13 

14     private ListView lv;

15     private ArrayAdapter<ListCellData> adapter;

16     

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.activity_main);

21         

22         adapter = new ArrayAdapter<ListCellData>(this, android.R.layout.simple_expandable_list_item_1);

23 //        adapter = new ArrayAdapter<String>(this, R.layout.lsit_cell);

24         

25         lv = (ListView) findViewById(R.id.listView1);

26         lv.setAdapter(adapter);

27         

28         adapter.add(new ListCellData("张三", "男", 45));

29         adapter.add(new ListCellData("李四", "男", 22));

30         adapter.add(new ListCellData("王五", "女", 33));

31         

32         lv.setOnItemClickListener(this);

33         

34 //        adapter.add("Hello");

35 //        adapter.add("What?");

36     }

37 

38     @Override

39     public void onItemClick(AdapterView<?> parent, View view, int position,

40             long id) {

41         ListCellData data = adapter.getItem(position);

42         

43         Toast.makeText(this, String.format("名字: %s, 性别: %s, 年龄: %d", data.getUserName(), data.getSex(), data.getAge()), Toast.LENGTH_SHORT).show();

44 

45         

46     }

47 

48 }
MainActivity

 

  带image的ListView:

07_ListView

 1 package com.example.listviewdemo;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.widget.ListView;

 6 

 7 public class CustomListView extends Activity{

 8     

 9     private ListView lv;

10     

11     @Override

12     protected void onCreate(Bundle savedInstanceState) {

13         // TODO Auto-generated method stub

14         super.onCreate(savedInstanceState);

15         setContentView(R.layout.custom_listview);

16         

17         lv = (ListView) findViewById(R.id.lv);

18         lv.setAdapter(new CustomListViewAdapter(this));        

19     }

20 

21 }
CustomListView
 1 package com.example.listviewdemo;

 2 

 3 import android.content.Context;

 4 import android.view.LayoutInflater;

 5 import android.view.View;

 6 import android.view.ViewGroup;

 7 import android.widget.BaseAdapter;

 8 import android.widget.ImageView;

 9 import android.widget.LinearLayout;

10 import android.widget.TextView;

11 

12 public class CustomListViewAdapter extends BaseAdapter {

13 

14     private Context context = null;

15     

16     CustomListViewAdapter(Context context){

17         this.context = context;

18     }

19     

20     public Context getContext() {

21         return context;

22     }

23     

24     private CustomListCellData[] data = new CustomListCellData[]{

25         new CustomListCellData("imag1", "dec 1", R.drawable.a),    

26         new CustomListCellData("imag2", "dec 2", R.drawable.b),    

27         new CustomListCellData("imag3", "dec 3", R.drawable.c)    

28     };

29     

30     @Override

31     public View getView(int position, View convertView, ViewGroup parent) {

32         

33 //        TextView tv = null;

34 //        

35 //        //这样做的好处:防止重复创建TextView

36 //        //在有view被回收后,convertView就不是null

37 //        if (convertView != null) {

38 //            tv = (TextView) convertView;

39 //        }else {

40 //            tv = new TextView(CustomListView.this);

41 //        }

42 //        

43 //        tv.setTextSize(30);

44 //        tv.setText(getItem(position));

45 //        

46 //        return tv;

47         

48         LinearLayout ll = null;

49         if (convertView != null) {

50             ll = (LinearLayout) convertView;

51         }else {

52             ll = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.custom_listv_cell, null);

53         }

54         

55         CustomListCellData data = getItem(position);

56         

57         ImageView icon = (ImageView) ll.findViewById(R.id.icon);

58         TextView name = (TextView) ll.findViewById(R.id.name);

59         TextView dec = (TextView) ll.findViewById(R.id.dec);

60         

61         icon.setImageResource(data.iconId);

62         name.setText(data.name);

63         dec.setText(data.dec);

64         

65         return ll;

66     }

67     

68     @Override

69     public long getItemId(int position) {

70         return position;

71     }

72     

73     @Override

74     public CustomListCellData getItem(int position) {

75         return data[position];

76     }

77     

78     @Override

79     public int getCount() {

80         return data.length;

81     }

82 

83 }
CustomListViewAdapter
 1 package com.example.listviewdemo;

 2 

 3 public class CustomListCellData {

 4 

 5     public CustomListCellData(String name, String dec, int iconId) {

 6         this.name = name;

 7         this.dec = dec;

 8         this.iconId = iconId;

 9     }

10 

11     public String name = "";

12     public String dec = "";

13     public int iconId = 0;

14 }
CustomListCellData
 1 <?xml version="1.0" encoding="utf-8"?>

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

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:orientation="horizontal" >

 6     

 7     <ImageView 

 8         android:layout_width="100dp" 

 9         android:layout_height="100dp"

10         android:id="@+id/icon"/>

11 

12     <LinearLayout 

13         android:layout_weight="1"

14         android:layout_width="wrap_content" 

15         android:layout_height="wrap_content"

16         android:orientation="vertical">

17 

18         <TextView

19             android:id="@+id/name"

20             android:layout_width="wrap_content"

21             android:layout_height="wrap_content"

22             android:text="Large Text"

23             android:textAppearance="?android:attr/textAppearanceLarge" />

24 

25         <TextView

26             android:id="@+id/dec"

27             android:layout_width="wrap_content"

28             android:layout_height="wrap_content"

29             android:text="TextView" />

30 

31     </LinearLayout>

32     

33 </LinearLayout>
AndroidManifest.xml

 

 

你可能感兴趣的:(ListView)