Android適配器Adapter學習 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

在開發中我們需要綁定一些數據展現到桌面上,這是就需要AdapterView。AdapterView是ViewGroup的子類,它決定瞭怎麼展現視圖通過Adapter來綁定特殊的數據類型。AdapterView是非常有幫助的當你展現數據在你的佈局中。Gallery,ListView和Spinner是AdapterView的子類。

 

      下面看一下AdapterView的結構圖:

           

 

 

      然後再看一下Adapter的結構圖:

 

    

 

    上面已經充分展現瞭他們的子類和父類的基礎關系。

 

     下面我們看一個ListViewDemo的例子:

 

    先來看一個簡單的adapter的例子:

 

  

 

 

public class SimpleList extends ListActivity { 

    private String[] mListString={"姓名:王魁鋒","性別:男","年齡:23", 

            "居住地:上海市普陀區","郵箱:wangkuifeng0118@126.com"}; 

    private ListView mListView=null; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        // TODO Auto-generated method stub 

        super.onCreate(savedInstanceState); 

        mListView=this.getListView(); 

        setListAdapter(new ArrayAdapter<String>(this, 

            android.R.layout.simple_list_item_1,mListString)); 

        mListView.setOnItemClickListener(new OnItemClickListener() { 

 

            @Override 

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

                    int position, long id) { 

                // TODO Auto-generated method stub 

            Toast.makeText(SimpleList.this, "你選擇瞭:"+mListString[position], 1).show(); 

            } 

        }); 

    } 

 

  這裡用到瞭系統定義好的適配模式,當然這隻能用來簡單的數據適配,下面看一下效果:

     

 

    接下來看一個稍微復雜點的,SimpleAdapter怎麼適配:

 

  

 

 

public class IconList extends ListActivity { 

 

     private String[] mListTitle = { "姓名", "性別", "年齡", "居住地","郵箱"};   

        private String[] mListStr = { "王魁鋒", "男", "23", "上海市普陀區",   

            "wangkuifeng0118@126.com"};   

        ListView mListView = null;   

        ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();;   

       

        @Override 

        protected void onCreate(Bundle savedInstanceState) { 

            // TODO Auto-generated method stub 

             mListView = getListView();   

                 

            int lengh = mListTitle.length;   

            for(int i =0; i < lengh; i++) {   

                Map<String,Object> item = new HashMap<String,Object>();   

                item.put("image", R.drawable.portrait);   

                item.put("title", mListTitle[i]);   

                item.put("text", mListStr[i]);   

                mData.add(item);    

            }   

            SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist,   

                new String[]{"image","title","text"},new int[]{R.id.image,R.id.title,R.id.text});    

                setListAdapter(adapter);   

            mListView.setOnItemClickListener(new OnItemClickListener() {   

                @Override 

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

                        int position, long id) { 

                    // TODO Auto-generated method stub 

                     Toast.makeText(IconList.this,"您選擇瞭標題:" + mListTitle[position] + "    內容:"+mListStr[position], Toast.LENGTH_LONG).show();   

                      

                }   

            });   

            super.onCreate(savedInstanceState); 

        } 

    上面的數據可以是同數據庫讀取的也可以是從網絡獲取的,這裡不做過多介紹,看下效果:

   

 

      哈哈 看起來美觀瞭些,如果要做更復雜的佈局,哪就要用BaseAdapter瞭。先看一下佈局文件:

 

 

 

<?xml version="1.0" encoding="utf-8"?>   

   

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

    android:layout_width="fill_parent" android:layout_height="wrap_content">   

    <ImageView android:id="@+id/color_image"   

        android:layout_width="wrap_content" android:layout_height="fill_parent"   

        android:layout_alignParentTop="true" android:layout_alignParentBottom="true"   

        android:adjustViewBounds="true"   

        android:padding="2dip" />   

    <TextView android:id="@+id/color_title"   

        android:layout_width="fill_parent" android:layout_height="wrap_content"   

        android:layout_toRightOf="@+id/color_image"   

        android:layout_alignParentTop="true"   

        android:layout_alignParentRight="true" android:singleLine="true"   

        android:ellipsize="marquee"    

        android:textSize="15dip"  />   

    <TextView android:id="@+id/color_text"   

        android:layout_width="fill_parent" android:layout_height="wrap_content"   

        android:layout_toRightOf="@+id/color_image"   

        android:layout_below="@+id/color_title"   

        android:layout_alignParentBottom="true"   

        android:layout_alignParentRight="true"    

        android:singleLine="true"   

        android:ellipsize="marquee"    

        android:textSize="20dip" />   

</RelativeLayout>   

  

下面是核心代碼:

 

 

 

public class ColorList extends ListActivity { 

     private String[] mListTitle = { "姓名", "性別", "年齡", "居住地","郵箱"};   

     private String[] mListText={"王魁鋒","男","23","上海市普陀區","wangkuifeng0118@126.com"}; 

     private ListView mListView=null; 

     private MyListAdapter myAdapter=null; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        // TODO Auto-generated method stub 

         mListView=this.getListView(); 

         myAdapter=new MyListAdapter(this); 

         this.setListAdapter(myAdapter); 

          

         mListView.setOnItemClickListener(new OnItemClickListener() { 

 

            @Override 

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

                    int position, long id) { 

                // TODO Auto-generated method stub 

                View v=parent.getChildAt(position); 

                v.setBackgroundColor(Color.RED); 

                Toast.makeText(ColorList.this, "你選擇瞭"+mListText[position], 1).show(); 

            } 

        }); 

        super.onCreate(savedInstanceState); 

    } 

    private class MyListAdapter extends BaseAdapter{ 

        private Context mContext; 

        private int[] colors=new int[]{0xff626569,0xff4f5257 }; 

         public MyListAdapter(Context context){ 

             mContext=context; 

         } 

        @Override 

        public int getCount() { 

            // TODO Auto-generated method stub 

            return mListText.length; 

        } 

 

        @Override 

        public Object getItem(int position) { 

            // TODO Auto-generated method stub 

            return position; 

        } 

 

        @Override 

        public long getItemId(int position) { 

            // TODO Auto-generated method stub 

            return position; 

        } 

 

        @Override 

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

            ImageView image=null; 

            TextView title=null; 

            TextView  content=null; 

            if(convertView==null){ 

                convertView=LayoutInflater.from(mContext).inflate(R.layout.colorlist, null); 

                image=(ImageView) convertView.findViewById(R.id.color_image); 

                title=(TextView) convertView.findViewById(R.id.color_title); 

                content=(TextView) convertView.findViewById(R.id.color_text); 

            } 

            int colorPos=position%colors.length; 

            convertView.setBackgroundColor(colors[colorPos]); 

            title.setText(mListTitle[position]); 

            content.setText(mListText[position]); 

            image.setImageResource(R.drawable.portrait); 

             

            return convertView; 

        } 

         

    } 

 

     BaseAdapter可以讓我們做比較復雜的佈局,隻要在xml文件中設置好佈局格式,在getView中分別取出放入相應的值就可以瞭。下面看一些效果:

  

 

    還有一些SpinnerAdapter和SimpleCursorAdapter等系統自帶的適配器,都是比較簡單的,可以看下API自行練習一下,這裡特別說明一下,從數據庫裡取出的數據最好直接放入SimpleCursorAdapter很方便的

 

摘自 wangkuifeng0118的專欄

發佈留言