亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺析Android之Adapter用法總結(jié)

 更新時(shí)間:2016年11月18日 15:23:40   作者:Devin Zhang  
本篇文章主要介紹了Android之Adapter用法總結(jié),具有一定的參考價(jià)值,有需要的可以了解一下。

1.概念

 Adapter是連接后端數(shù)據(jù)和前端顯示的適配器接口,是數(shù)據(jù)和UI(View)之間一個(gè)重要的紐帶。在常見的View(ListView,GridView)等地方都需要用到Adapter。如下圖直觀的表達(dá)了Data、Adapter、View三者的關(guān)系:
Android中所有的Adapter一覽:
 由圖可以看到在Android中與Adapter有關(guān)的所有接口、類的完整層級(jí)圖。在我們使用過程中可以根據(jù)自己的需求實(shí)現(xiàn)接口或者繼承類進(jìn)行一定的擴(kuò)展。比較常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。

  • BaseAdapter是一個(gè)抽象類,繼承它需要實(shí)現(xiàn)較多的方法,所以也就具有較高的靈活性;
  • ArrayAdapter支持泛型操作,最為簡(jiǎn)單,只能展示一行字。
  • SimpleAdapter有最好的擴(kuò)充性,可以自定義出各種效果。
  • SimpleCursorAdapter可以適用于簡(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)容以列表的形式展示出來。

2.應(yīng)用案例

1)ArrayAdapter

列表的顯示需要三個(gè)元素:

a.ListVeiw 用來展示列表的View。

b.適配器 用來把數(shù)據(jù)映射到ListView上的中介。

c.?dāng)?shù)據(jù)    具體的將被映射的字符串,圖片,或者基本組件。

案例一

public class ArrayAdapterActivity extends ListActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   //列表項(xiàng)的數(shù)據(jù)
   String[] strs = {"1","2","3","4","5"};
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strs);
   setListAdapter(adapter);
  }
 }

 案例二

 public class MyListView extends Activity {
 
  private ListView listView;
  //private List<String> data = new ArrayList<String>();
  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
    
   listView = new ListView(this);
   listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
   setContentView(listView);
  }
   
  private List<String> getData(){
    
   List<String> data = new ArrayList<String>();
   data.add("測(cè)試數(shù)據(jù)1");
   data.add("測(cè)試數(shù)據(jù)2");
   data.add("測(cè)試數(shù)據(jù)3");
   data.add("測(cè)試數(shù)據(jù)4");
    
   return data;
  }
 }

上面代碼使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數(shù)據(jù),要裝配這些數(shù)據(jù)就需要一個(gè)連接ListView視圖對(duì)象和數(shù)組數(shù)據(jù)的適配器來兩者的適配工作,ArrayAdapter的構(gòu)造需要三個(gè)參數(shù),依次為this,布局文件(注意這里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系統(tǒng)定義好的布局文件只顯示一行文字,數(shù)據(jù)源(一個(gè)List集合)。同時(shí)用setAdapter()完成適配的最后工作。效果圖如下:

2)SimpleAdapter

simpleAdapter的擴(kuò)展性最好,可以定義各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(復(fù)選框)等等。下面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差別,不同就是對(duì)顯示ListView做了許多優(yōu)化,方面顯示而已。

案例一

simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textColor="#ffffff"
android:textSize="20sp"
/>
</LinearLayout>
public class SimpleAdapterActivity extends ListActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   
   SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[] { "title", "img" }, new int[] { R.id.title, R.id.img });
   setListAdapter(adapter);
  }
  
  private List<Map<String, Object>> getData() {
   //map.put(參數(shù)名字,參數(shù)值)
   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("title", "摩托羅拉");
   map.put("img", R.drawable.icon);
   list.add(map);
   
   map = new HashMap<String, Object>();
   map.put("title", "諾基亞");
   map.put("img", R.drawable.icon);
   list.add(map);
   
   map = new HashMap<String, Object>();
   map.put("title", "三星");
   map.put("img", R.drawable.icon);
   list.add(map);
   return list;
   } 
  
 }

 案例二

下面的程序是實(shí)現(xiàn)一個(gè)帶有圖片的類表。首先需要定義好一個(gè)用來顯示每一個(gè)列內(nèi)容的xml,vlist.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="fill_parent"> 
  <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/>
  <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
   <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="#FFFFFFFF" android:textSize="22px" />
   <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="#FFFFFFFF" android:textSize="13px" />
  </LinearLayout>
  </LinearLayout>
 public class MyListView3 extends ListActivity {
  // private List<String> data = new ArrayList<String>();
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  
   SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
     new String[]{"title","info","img"},
     new int[]{R.id.title,R.id.info,R.id.img});
   setListAdapter(adapter);
  }
  
  private List<Map<String, Object>> getData() {
   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("title", "G1");
   map.put("info", "google 1");
   map.put("img", R.drawable.i1);
   list.add(map);
  
   map = new HashMap<String, Object>();
   map.put("title", "G2");
   map.put("info", "google 2");
   map.put("img", R.drawable.i2);
   list.add(map);
  
   map = new HashMap<String, Object>();
   map.put("title", "G3");
   map.put("info", "google 3");
   map.put("img", R.drawable.i3);
   list.add(map);
    
   return list;
  }
 }

使用simpleAdapter的數(shù)據(jù)用一般都是HashMap構(gòu)成的List,list的每一節(jié)對(duì)應(yīng)ListView的每一行。HashMap的每個(gè)鍵值數(shù)據(jù)映射到布局文件中對(duì)應(yīng)id的組件上。因?yàn)橄到y(tǒng)沒有對(duì)應(yīng)的布局文件可用,我們可以自己定義一個(gè)布局vlist.xml。下面做適配,new一個(gè)SimpleAdapter參數(shù)一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的組件id,title,info,img。布局文件的各組件分別映射到HashMap的各元素上,完成適配。

運(yùn)行效果如下圖:

3)SimpleCursorAdapter

public class SimpleCursorAdapterActivity extends ListActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   //獲得一個(gè)指向系統(tǒng)通訊錄數(shù)據(jù)庫的Cursor對(duì)象獲得數(shù)據(jù)來源
   Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
   startManagingCursor(cur);
   //實(shí)例化列表適配器
   
   ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {People.NAME}, new int[] {android.R.id.text1});
   setListAdapter(adapter);
  }
 }

 一定要以數(shù)據(jù)庫作為數(shù)據(jù)源的時(shí)候,才能使用SimpleCursorAdapter,這里特別需要注意的一點(diǎn)是:不要忘了在AndroidManifest.xml文件中加入權(quán)限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

效果如下:

4)BaseAdapter

有時(shí)候,列表不光會(huì)用來做顯示用,我們同樣可以在在上面添加按鈕。添加按鈕首先要寫一個(gè)有按鈕的xml文件,然后自然會(huì)想到用上面的方法定義一個(gè)適配器,然后將數(shù)據(jù)映射到布局文件上。但是事實(shí)并非這樣,因?yàn)榘粹o是無法映射的,即使你成功的用布局文件顯示出了按鈕也無法添加按鈕的響應(yīng),這時(shí)就要研究一下ListView是如何現(xiàn)實(shí)的了,而且必須要重寫一個(gè)類繼承BaseAdapter。下面的示例將顯示一個(gè)按鈕和一個(gè)圖片,兩行字如果單擊按鈕將刪除此按鈕的所在行。并告訴你ListView究竟是如何工作的。

vlist2.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/>
  <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
   <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="#FFFFFFFF" android:textSize="22px" />
   <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="#FFFFFFFF" android:textSize="13px" />
  </LinearLayout>

  <Button android:id="@+id/view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:text="@string/s_view_btn" android:layout_gravity="bottom|right" />
 </LinearLayout>
/**
 * @author 
 *
 */
 public class MyListView4 extends ListActivity {
 
 
  private List<Map<String, Object>> mData;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   mData = getData();
   MyAdapter adapter = new MyAdapter(this);
   setListAdapter(adapter);
  }
 
  private List<Map<String, Object>> getData() {
   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("title", "G1");
   map.put("info", "google 1");
   map.put("img", R.drawable.i1);
   list.add(map);
 
   map = new HashMap<String, Object>();
   map.put("title", "G2");
   map.put("info", "google 2");
   map.put("img", R.drawable.i2);
   list.add(map);
 
   map = new HashMap<String, Object>();
   map.put("title", "G3");
   map.put("info", "google 3");
   map.put("img", R.drawable.i3);
   list.add(map);
   
   return list;
  }
  
  // ListView 中某項(xiàng)被選中后的邏輯
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
   
   Log.v("MyListView4-click", (String)mData.get(position).get("title"));
  }
  
  /**
  * listview中點(diǎn)擊按鍵彈出對(duì)話框
  */
  public void showInfo(){
   new AlertDialog.Builder(this)
   .setTitle("我的listview")
   .setMessage("介紹...")
   .setPositiveButton("確定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
   })
   .show();
   
  }
  
  
  
  public final class ViewHolder{
   public ImageView img;
   public TextView title;
   public TextView info;
   public Button viewBtn;
  }
  
  
  public class MyAdapter extends BaseAdapter{
 
   private LayoutInflater mInflater;
   
   
   public MyAdapter(Context context){
    this.mInflater = LayoutInflater.from(context);
   }
   @Override
   public int getCount() {
    // TODO Auto-generated method stub
    return mData.size();
   }
 
   @Override
   public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
   }
 
   @Override
   public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
   }
 
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    
    ViewHolder holder = null;
    if (convertView == null) {
     
     holder=new ViewHolder(); 
     
     convertView = mInflater.inflate(R.layout.vlist2, null);
     holder.img = (ImageView)convertView.findViewById(R.id.img);
     holder.title = (TextView)convertView.findViewById(R.id.title);
     holder.info = (TextView)convertView.findViewById(R.id.info);
     holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
     convertView.setTag(holder);
     
    }else {
     
     holder = (ViewHolder)convertView.getTag();
    }
    
    
    holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
    holder.title.setText((String)mData.get(position).get("title"));
    holder.info.setText((String)mData.get(position).get("info"));
    
    holder.viewBtn.setOnClickListener(new View.OnClickListener() {
     
     @Override
     public void onClick(View v) {
      showInfo();    
     }
    });
    
    
    return convertView;
   }
   
  }  
 }

下面將對(duì)上述代碼,做詳細(xì)的解釋,listView在開始繪制的時(shí)候,系統(tǒng)首先調(diào)用getCount()函數(shù),根據(jù)他的返回值得到listView的長度(這也是為什么在開始的第一張圖特別的標(biāo)出列表長度),然后根據(jù)這個(gè)長度,調(diào)用getView()逐一繪制每一行。如果你的getCount()返回值是0的話,列表將不顯示同樣return 1,就只顯示一行。

系統(tǒng)顯示列表時(shí),首先實(shí)例化一個(gè)適配器(這里將實(shí)例化自定義的適配器)。當(dāng)手動(dòng)完成適配時(shí),必須手動(dòng)映射數(shù)據(jù),這需要重寫getView()方法。系統(tǒng)在繪制列表的每一行的時(shí)候?qū)⒄{(diào)用此方法。getView()有三個(gè)參數(shù),position表示將顯示的是第幾行,covertView是從布局文件中inflate來的布局。我們用LayoutInflater的方法將定義好的vlist2.xml文件提取成View實(shí)例用來顯示。然后將xml文件中的各個(gè)組件實(shí)例化(簡(jiǎn)單的findViewById()方法)。這樣便可以將數(shù)據(jù)對(duì)應(yīng)到各個(gè)組件上了。但是按鈕為了響應(yīng)點(diǎn)擊事件,需要為它添加點(diǎn)擊監(jiān)聽器,這樣就能捕獲點(diǎn)擊事件。至此一個(gè)自定義的listView就完成了,現(xiàn)在讓我們回過頭從新審視這個(gè)過程。系統(tǒng)要繪制ListView了,他首先獲得要繪制的這個(gè)列表的長度,然后開始繪制第一行,怎么繪制呢?調(diào)用getView()函數(shù)。在這個(gè)函數(shù)里面首先獲得一個(gè)View(實(shí)際上是一個(gè)ViewGroup),然后再實(shí)例并設(shè)置各個(gè)組件,顯示之。好了,繪制完這一行了。那再繪制下一行,直到繪完為止。在實(shí)際的運(yùn)行過程中會(huì)發(fā)現(xiàn)listView的每一行沒有焦點(diǎn)了,這是因?yàn)锽utton搶奪了listView的焦點(diǎn),只要布局文件中將Button設(shè)置為沒有焦點(diǎn)就OK了。

效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android手機(jī)顯示多彩霓虹燈效果

    Android手機(jī)顯示多彩霓虹燈效果

    這篇文章主要為大家詳細(xì)介紹了Android手機(jī)顯示多彩霓虹燈效果的小實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android使用TypeFace設(shè)置TextView的文字字體

    Android使用TypeFace設(shè)置TextView的文字字體

    這篇文章主要介紹了Android使用TypeFace設(shè)置TextView的文字字體的方法,幫助大家更好的利用Android開發(fā),感興趣的朋友可以了解下
    2021-01-01
  • 編寫簡(jiǎn)易Android天氣應(yīng)用的代碼示例

    編寫簡(jiǎn)易Android天氣應(yīng)用的代碼示例

    這篇文章主要介紹了編寫簡(jiǎn)易Android天氣應(yīng)用的代碼示例,文中的例子主要是利用到了RxAndroid處理異步方法,需要的朋友可以參考下
    2016-02-02
  • Android程序退出完美解決方案兼容所有SDK

    Android程序退出完美解決方案兼容所有SDK

    本文將介紹Android程序退出完美解決實(shí)現(xiàn)方法,兼容所有SDK,需要的朋友可以參考下
    2012-11-11
  • Android Scroller的使用方法

    Android Scroller的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android Scroller的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android內(nèi)存泄漏導(dǎo)致原因深入探究

    Android內(nèi)存泄漏導(dǎo)致原因深入探究

    內(nèi)存管理的目的就是讓我們?cè)陂_發(fā)過程中有效避免我們的應(yīng)用程序出現(xiàn)內(nèi)存泄露的問題。內(nèi)存泄露相信大家都不陌生,我們可以這樣理解:沒有用的對(duì)象無法回收的現(xiàn)象就是內(nèi)存泄露
    2023-02-02
  • Android AMS啟動(dòng)詳解

    Android AMS啟動(dòng)詳解

    這篇文章主要介紹了Android AMS啟動(dòng)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android整理需要翻譯的strings資源詳情

    Android整理需要翻譯的strings資源詳情

    這篇文章主要介紹了Android整理需要翻譯的strings資源,文章主要列出所有res目錄,根據(jù)是否包含values-ru分成兩組解紹,需要的朋友可以參考一下
    2021-10-10
  • Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器

    Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第一篇,如何實(shí)現(xiàn)最簡(jiǎn)單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android 存儲(chǔ)路徑選擇方法

    Android 存儲(chǔ)路徑選擇方法

    今天小編就為大家分享一篇Android 存儲(chǔ)路徑選擇方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評(píng)論