知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
Android筆記十一.ListView+Adapter
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):44
深刻懂得Adapter
一、ListView
ListView是Android開辟過程中較為常見的組件之一,它將數(shù)據(jù)以列表的情勢(shì)展示出來。一般而言,一個(gè)ListView由以下三個(gè)元素構(gòu)成:
1.View:展示列表視圖,平日是一個(gè)xml所指定的。大年夜家都知道Android的界面根本上是由xml文件負(fù)責(zé)完成的,所以ListView的界面也理所應(yīng)當(dāng)?shù)膽?yīng)用了xml定義。例如在ListView中經(jīng)常用到的“android.R.layout.simple_list_item_1”等, 就是Android體系內(nèi)部定義好的一個(gè)xml文件。
2.Adapter適配器:用于將不合的數(shù)據(jù)映射到View上。不合的數(shù)據(jù)對(duì)應(yīng)不合的適配器,如ArrayAdapter,CursorAdapter, SimpleAdapter等, 他們可以或許將數(shù)組,指針指向的數(shù)據(jù),Map等數(shù)據(jù)或根本組件映射到View上。也恰是因?yàn)檫m配器的存在,使得ListView的應(yīng)用相當(dāng)靈活,經(jīng)由適配器的處理后,在 view看來所有的數(shù)據(jù)映射過來都是一樣的。
3.數(shù)據(jù):具體的數(shù)據(jù)和資本??伤宰址畧D片、Map映射等,經(jīng)由過程適配器這些數(shù)據(jù)將會(huì)被實(shí)際到 ListView上。
總之,所有的數(shù)據(jù)和資本要顯示到ListView上都經(jīng)由過程適配器來完成。ArrayAdapter,CursorAdapter, SimpleAdapter為體系已有的適配器,可以將如數(shù)組、Cursor指向的數(shù)據(jù)(一般為數(shù)據(jù)庫)、Map里的數(shù)據(jù)。
二、深刻懂得Adapter
1.概要:Adapter是連接后端數(shù)據(jù)和前端視圖顯示的適配寡居口,是數(shù)據(jù)和UI(View)之間的橋梁。經(jīng)由過程適配器,可以將不合的數(shù)據(jù)映射并顯示到View上。在常見的View(ListView、GridView)等處所都須要用到Adapter。對(duì)于Data、Adapter、View的關(guān)系,如下圖所示:
[img]http://img.blog.csdn.net/20150104172857879?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
2.Android中Adapter類
(1)BaseAdapter適配器:為一個(gè)抽象類,應(yīng)用該類可以實(shí)現(xiàn)更為復(fù)雜的ListView。經(jīng)由過程文檔可以看出,ArrayAdapter,CursorAdapter, SimpleAdapter都持續(xù)于BaseAdapter。所以經(jīng)由過程持續(xù)BaseAdapter就可以完成本身的Adapter,可以將任何復(fù)雜組合的數(shù)據(jù)和資本,以任何你想要的顯示效不雅展示處理。持續(xù)BaseAdapter之后,須要重寫以下四個(gè)辦法:getCount,getItem,getItemId,getView。然則,ArrayAdapter,CursorAdapter, SimpleAdapter則無須要全部重寫上述四種辦法。
(2)ArrayAdapter適配器:支撐泛型操作,最為簡(jiǎn)單,只能展示一行字符串。
(3)SimpleAdapter適配器:有最好的擴(kuò)充性,可以自定義出各類效不雅(字符串、圖片、Map集),平日應(yīng)用xml構(gòu)造文件來指定視圖。
(4)SimpleCursorAdapter適配器:可以實(shí)用于簡(jiǎn)單的純文字型ListView,它須要Cursor的字段和UI的id對(duì)應(yīng)起來。如須要實(shí)現(xiàn)更復(fù)雜的UI也可以重寫其他辦法??梢哉J(rèn)為是SimpleAdapter對(duì)數(shù)據(jù)庫的簡(jiǎn)單結(jié)合,可以便利地把數(shù)據(jù)庫的內(nèi)容以列表的情勢(shì)展示出來。
[img]http://img.blog.csdn.net/20150104172917863?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
[img]http://blog.csdn.net/u012637501/article/details/42394937/file:/C:/Users/Jiangdongguo/AppData/Local/YNote/data/[email protected]/8c6f681bdb74433e80b77f7e7fa0919c/clipboard.png
3.ListView加載adapter一般過程
(1)先斷定adapter有若干癟據(jù)項(xiàng),根據(jù)這個(gè)數(shù)據(jù)肯定有若干item(列表項(xiàng)). -----getCount()辦法實(shí)現(xiàn)
(2)肯定每個(gè)item里加載哪個(gè)View. -----getView()辦法實(shí)現(xiàn)
(3)向?qū)?yīng)的View中加載須要顯示的數(shù)據(jù)
4.ArrayAdapter實(shí)例分析
(1)開辟根本思路
ArrayAdapter適配器支撐泛型操作,列表的每一項(xiàng)只能顯示一串字符,不克不及顯示圖片等資本數(shù)據(jù)。
*應(yīng)用字符串?dāng)?shù)組作為數(shù)據(jù)源
a.實(shí)現(xiàn)一個(gè)字符串?dāng)?shù)組作為數(shù)據(jù)源(List集)(數(shù)組元素有幾個(gè),就有幾個(gè)列表項(xiàng))
b.實(shí)例化一個(gè)ArrayAdapter<String> 適配器對(duì)象,用于裝配數(shù)據(jù)源到指定的View.
ArrayAdapter<String> adapter=new ArrayAdapter<String>( this,android.R.layout.simple_expandable_list_item_1, strs);
c.將ArrayAdapter實(shí)用于ListActivity的ListView
setListAdapter(adapter);
*應(yīng)用List集合(ArrayList)作為數(shù)據(jù)源
a.實(shí)例化一個(gè)ListView對(duì)象
b.實(shí)例化一個(gè)ArrayAdapter<String>對(duì)象(集合中數(shù)據(jù)項(xiàng)類型為String),將Map集合中的數(shù)據(jù)項(xiàng)映射到列表項(xiàng)中;
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData());
c.裝配Adapter到ListView中并顯示ListView視圖
listview.setAdapter(adapter); //裝配Adapter到ListView
setContentView(listview); //將list視圖顯示
(2)源代碼分析一
MainActivt.java
package com.example.android_arraylist_1;
mport android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] strs={"第一項(xiàng)","第二項(xiàng)","第三項(xiàng)","第四項(xiàng)"}; //列表項(xiàng)數(shù)據(jù)
ArrayAdapter<String> adapter=new ArrayAdapter<String>( //實(shí)例化一個(gè)ArrayAdapter適配器來裝配數(shù)據(jù)
this, //高低文
android.R.layout.simple_expandable_list_item_1, //列表的每一行構(gòu)造,只顯示一行文字
strs); //數(shù)據(jù)源(一個(gè)List集合)
setListAdapter(adapter); //將List集合數(shù)據(jù)顯示在List視圖中
}
}
分析:
膳綾擎代碼應(yīng)用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數(shù)據(jù),要裝拍┞封些數(shù)據(jù)就須要一個(gè)連接ListView視圖對(duì)象和數(shù)組數(shù)據(jù)的適配器來兩者的適配工作,ArrayAdapter的構(gòu)造須要三個(gè)參數(shù),依次為this,構(gòu)造文件(留意這里的構(gòu)造文件描述的昵嘈表的每一行的構(gòu)造,android.R.layout.simple_list_item_1是體系定義好的構(gòu)造文件只顯示一行文字,數(shù)據(jù)源(一個(gè)List集合)。同時(shí)用setAdapter()完成適配的最后工作。
效不雅:
[img]http://blog.csdn.net/u012637501/article/details/42394937/file:/C:/Users/Jiangdongguo/AppData/Local/YNote/data/[email protected]/156392813d6b4f5caf91c08aa431dfa6/clipboard.png[img]http://img.blog.csdn.net/20150104172945740?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
(3)源碼分析二
ArrayListActivity.java
package com.example.android_arraylist_2;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ArrayListActivity extends Activity {
private ListView listview; //定義一個(gè)ListView對(duì)象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listview =new ListView(this); //實(shí)例化一個(gè)ListView對(duì)象
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, //實(shí)例化一個(gè)適配器adapter
android.R.layout.simple_expandable_list_item_1, //構(gòu)造文件:顯示一行文字
getData()); //獲取數(shù)據(jù)源(ArrayList集合)
listview.setAdapter(adapter); //裝配Adapter到ListView
setContentView(listview); //將list視圖顯示
}
private List<String> getData() {
// TODO Auto-generated method stub
List<String> data=http://www.sjsjw.com/100/000269MYM030665/new ArrayList
data.add("測(cè)試數(shù)據(jù)1"); //向ArrayList集合中添加元素?cái)?shù)據(jù)
data.add("測(cè)試數(shù)據(jù)2");
data.add("測(cè)試數(shù)據(jù)3");
data.add("測(cè)試數(shù)據(jù)4");
return data; //返回ArrayList集合對(duì)象
}
}
分析:
(1) getData()
(2) List<String>
(3) List<String> data=http://www.sjsjw.com/100/000269MYM030665/new ArrayList
效不雅:
[img]http://blog.csdn.net/u012637501/article/details/42394937/file:/C:/Users/Jiangdongguo/AppData/Local/YNote/data/[email protected]/38d11e5cd42c471fb98d66a64e3fe91f/clipboard.png[img]http://img.blog.csdn.net/20150104173014421?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
5.SimpleAdapter實(shí)例分析
與ArrayAdapter不合的是,simpleAdapter的擴(kuò)大性最好,可以定義各類各樣的構(gòu)造出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(復(fù)選框)等等。一般情況下,我們都是應(yīng)用Map哈希表作為數(shù)據(jù)源,經(jīng)由過程鍵到組件Id的映射實(shí)現(xiàn)列表項(xiàng)內(nèi)容。
(1)開辟根本思路
a.裝配適配器,將Map鍵數(shù)據(jù)資本竽暌鉤射到Id所對(duì)應(yīng)的組件上;
SimpleAdapter adapter =new SimpleAdapter(this, //高低文
getData(), //獲取list數(shù)據(jù)集
R.layout.simple, //指定構(gòu)造文件
new String[]{"img","title1","info"}, //Map集合鍵
new int[]{R.id.img,R.id.title1,R.id.info});//界面組件Id
b.實(shí)現(xiàn)一個(gè)Map集合,并將map對(duì)象添加ArrayList中
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); //實(shí)例化一個(gè)ArrayList對(duì)象,
Map<String,Object> map=new HashMap<String,Object>(); //設(shè)置列表第一項(xiàng)標(biāo)題與擱筆(應(yīng)用Map)
c.將ArrayAdapter實(shí)用于ListActivity的ListView
setListAdapter(adapter);
(2)源代碼分析
SimpleAdapterActivity.java
package com.example.android_sampleadapter_1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class SimpleAdapterActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.裝配適配器,并綁定命據(jù)到響應(yīng)的視圖
SimpleAdapter adapter =new SimpleAdapter(this, //高低文
getData(), //獲取list數(shù)據(jù)集
R.layout.simple, //指定構(gòu)造文件
new String[]{"img","title1","info"}, //Map集合鍵
new int[]{R.id.img,R.id.title1,R.id.info});//界面組件Id
//2.顯示視圖
setListAdapter(adapter);
}
//3.Map數(shù)據(jù)集:HashMap構(gòu)成的List,list的每一節(jié)對(duì)應(yīng)ListView的每一行
//HashMap的每個(gè)鍵值數(shù)據(jù)映射到構(gòu)造文件中對(duì)應(yīng)id的組件上
private List<Map<String,Object>> getData() {
// TODO Auto-generated method stub
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); //實(shí)例化一個(gè)ArrayList對(duì)象,
Map<String,Object> map=new HashMap<String,Object>(); //設(shè)置列表第一項(xiàng)標(biāo)題與擱筆(應(yīng)用Map)
map.put("img", R.drawable.a);
map.put("title1", "華為光榮");
map.put("info", "學(xué)人家玩搶購,真TM坑爹!");
list.add(map); //將一個(gè)map對(duì)象添加到列表中
map=new HashMap<String,Object>();
map.put("img", R.drawable.b);
map.put("title1", "中國移動(dòng)");
map.put("info", "god knows");
list.add(map); //將一個(gè)map對(duì)象添加到列表中
map=new HashMap<String,Object>();
map.put("img", R.drawable.c);
map.put("title1", "中興");
map.put("info", "手機(jī)還不錯(cuò),只是要逝世不活的");
list.add(map); //將一個(gè)map對(duì)象添加到列表中
map=new HashMap<String,Object>();
map.put("img", R.drawable.d);
map.put("title1", "魅族4");
map.put("info", "有點(diǎn)貴!");
list.add(map); //將一個(gè)map對(duì)象添加到列表中
return list;
}
}
分析:
應(yīng)用simpleAdapter的數(shù)據(jù)用一般都是HashMap構(gòu)成的List,list的每一節(jié)對(duì)應(yīng)ListView的每一行。HashMap的每個(gè)鍵值數(shù)據(jù)映射到構(gòu)造文件中對(duì)應(yīng)id的組件上。因?yàn)轶w系沒有對(duì)應(yīng)的構(gòu)造文件可用,我們可以本身定義一個(gè)構(gòu)造simple.xml。下面做適配,new一個(gè)SimpleAdapter參數(shù)一次是:this,構(gòu)造文件(vlist.xml),HashMap的 title 和 info,img,構(gòu)造文件的組件id,title,info,img。構(gòu)造文件的各組件分別映射到HashMap的各元素上,完成適配。
simple.xml-列表項(xiàng)構(gòu)造文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000" android:textSize="22sp"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000" android:textSize="13sp"/>
</LinearLayout>
</LinearLayout>
[img]http://img.blog.csdn.net/20150104173029171?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
6.SimpleAdapter實(shí)例分析
package com.example.android_simplecuroradapter;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
public class SimpleCursorActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.獲得一個(gè)指向體系通信錄數(shù)據(jù)庫的CurSor對(duì)象獲得數(shù)據(jù)來源
Cursor cur=getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
//2.實(shí)例化列表適配器
ListAdapter adapter=new SimpleCursorAdapter(this,android.R.layout.simple_expandable_list_item_1,cur,new String[] {People.NAME},new int[] {android.R.id.text1});
//3.顯示視圖
setListAdapter(adapter);
}
}
分析:
必定要以數(shù)據(jù)庫作為數(shù)據(jù)源的時(shí)刻,才能應(yīng)用SimpleCursorAdapter,這里特別須要留意的一點(diǎn)是:不要忘了在AndroidManifest.xml文件中參加權(quán)限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
效不雅:
[img]http://img.blog.csdn.net/20150104173025270?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
[img]http://blog.csdn.net/u012637501/article/details/42394937/file:/C:/Users/Jiangdongguo/AppData/Local/YNote/data/[email protected]/af561294048d44c59f1f6a1280fea6f1/clipboard.png
參考:
1.http://www.cnblogs.com/Gaojiecai/archive/2012/10/08/2715962.html
2.http://wenku.baidu.com/link?url=1ln6lAvzN9E-1eOuEfy2E_i1O0Ws8gE0KcySX-yu-j7wEzgd1EkuDhwcfeCcM48egMIFb-Egm7mh1RODs9hkXw8C1zjqNkIa4lyvH5oliHO
3.http://wenku.baidu.com/view/0224ead8ad51f01dc281f141.html
相關(guān)案例查看更多
相關(guān)閱讀
- 云南建設(shè)廳網(wǎng)站
- 報(bào)廢車回收管理系統(tǒng)
- 昆明小程序哪家好
- 小程序被騙退款成功
- 昆明小程序定制開發(fā)
- 云南小程序開發(fā)課程
- 云南軟件公司
- 網(wǎng)站維護(hù)
- 云南電商網(wǎng)站建設(shè)
- 云南小程序哪家好
- flex
- 云南小程序被騙
- 旅游網(wǎng)站建設(shè)
- 北京小程序制作
- SEO
- 汽車報(bào)廢回收
- 小程序商城
- 人人商城
- 云南網(wǎng)頁制作
- 云南網(wǎng)站建設(shè)費(fèi)用
- 云南網(wǎng)站開發(fā)哪家好
- 商標(biāo)注冊(cè)
- 昆明軟件定制公司
- 云南省住房建設(shè)廳網(wǎng)站
- 云南小程序設(shè)計(jì)
- 保險(xiǎn)網(wǎng)站建設(shè)公司
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)
- 網(wǎng)站建設(shè)首選
- 小程序開發(fā)費(fèi)用
- 小程序開發(fā)聯(lián)系方式