日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

[Android学习笔记]使用ListView

發(fā)布時間:2023/12/15 Android 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Android学习笔记]使用ListView 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡單使用ListView


?

關(guān)鍵在于Adatper

Adatper用來連接UI與數(shù)據(jù)源。Adapter既負(fù)責(zé)提供數(shù)據(jù),又負(fù)責(zé)創(chuàng)建Item視圖。


?

一般步驟:

1.創(chuàng)建list_item.xml,用來創(chuàng)建ListView的Item的UI

2.自定義Adapter和數(shù)據(jù)源對象

3.在頁面布局中定義ListView,在Activity中獲取ListView引用

4.為ListView添加Adatper對象


?

?

Ex:

1.創(chuàng)建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 {/*** 數(shù)據(jù)源 */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)* 創(chuàng)建Item視圖,關(guān)聯(lián)數(shù)據(jù)源*/@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

?

轉(zhuǎn)載于:https://www.cnblogs.com/hellenism/p/3617058.html

總結(jié)

以上是生活随笔為你收集整理的[Android学习笔记]使用ListView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。