[Android学习笔记]使用ListView
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                [Android学习笔记]使用ListView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                簡單使用ListView
?
關鍵在于Adatper
Adatper用來連接UI與數據源。Adapter既負責提供數據,又負責創建Item視圖。
?
一般步驟:
1.創建list_item.xml,用來創建ListView的Item的UI
2.自定義Adapter和數據源對象
3.在頁面布局中定義ListView,在Activity中獲取ListView引用
4.為ListView添加Adatper對象
?
?
Ex:
1.創建list_item.xml
<?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="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/name"android:paddingLeft="50px"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/age"android:text="a"android:paddingLeft="50px"/><TextViewandroid:id="@+id/id"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="50px" /></LinearLayout> list_item.xml?
2.自定義Adatper
public class StudentAdapter extends BaseAdapter {/*** 數據源 */private List<Student> students;/*** inflater引用 ,用來加載item.xml,獲得view引用*/private LayoutInflater inflater;/*** item.xml資源 */private int source;public StudentAdapter(Context context,List<Student> students , int source){this.students = students;this.source = source;//inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn students.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn students.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}/* (non-Javadoc)* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)* 創建Item視圖,關聯數據源*/@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stub TextView idView = null;TextView nameView = null;TextView ageView = null;if(arg1 == null){arg1 = this.inflater.inflate(source, null);idView = (TextView)arg1.findViewById(R.id.id);nameView =(TextView)arg1.findViewById(R.id.name);ageView = (TextView)arg1.findViewById(R.id.age);}else{idView = (TextView)arg1.findViewById(R.id.id);nameView =(TextView)arg1.findViewById(R.id.name);ageView = (TextView)arg1.findViewById(R.id.age);}Student stu = students.get(arg0);idView.setText(stu.getId().toString());nameView.setText(stu.getName());ageView.setText(stu.getAge().toString());return arg1;}} Adatper?
3.在Activity中為ListView添加Adatper
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView)findViewById(R.id.myListView);ArrayList<Student> students = new ArrayList<Student>();students.add(new Student("st",1,1));StudentAdapter aa = new StudentAdapter(this,students,R.layout.list_item);listView.setAdapter(aa);} Activity?
轉載于:https://www.cnblogs.com/hellenism/p/3617058.html
總結
以上是生活随笔為你收集整理的[Android学习笔记]使用ListView的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: vm安装winme[通俗易懂](Wind
- 下一篇: 人力资源从业人员:使用“算法裁员”可抚平
