最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java ArrayAdapter用法案例詳解

 更新時(shí)間:2021年08月23日 17:34:29   作者:文醬  
這篇文章主要介紹了Java ArrayAdapter用法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

      拖延癥最可怕的地方就是:就算自己這邊沒(méi)有拖延,但對(duì)方也會(huì)拖延,進(jìn)而導(dǎo)致自己這邊也開(kāi)始拖延起來(lái)!現(xiàn)在這個(gè)項(xiàng)目我這邊已經(jīng)是完工了,但是對(duì)方遲遲沒(méi)有搞定,導(dǎo)致整個(gè)項(xiàng)目無(wú)法提交。

     這就是拖延癥的可怕:我們不僅是與自己的拖延癥作戰(zhàn),而是與所有有關(guān)人士的拖延癥作戰(zhàn),決定項(xiàng)目是否能夠提交,在于那個(gè)最慢的人。

     既然決定權(quán)已經(jīng)不在我的手上,那么我也可以做做其他事情,像是現(xiàn)在這樣寫(xiě)寫(xiě)博客。

    這次就介紹一下ListView中比較簡(jiǎn)單但又非常方便的ArrayAdapter。

    ArrayAdapter是BaseAdapter的派生類,在BaseAdapter的基礎(chǔ)上,添加了一項(xiàng)重大的功能:可以直接使用泛型構(gòu)造。

    我們先來(lái)看一個(gè)簡(jiǎn)單的例子:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) this.findViewById(R.id.list);
        UserAdapter adapter = new UserAdapter(this, R.layout.list_item);
        adapter.add(new User(10, "小智", "男"));
        adapter.add(new User(10, "小霞", "女"));
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class UserAdapter extends ArrayAdapter<User> {
        private int mResourceId;

        public UserAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
            this.mResourceId = textViewResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            User user = getItem(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(mResourceId, null);
            TextView nameText = (TextView) view.findViewById(R.id.name);
            TextView ageText = (TextView) view.findViewById(R.id.age);
            TextView sexText = (TextView) view.findViewById(R.id.sex);

            nameText.setText(user.getName());
            ageText.setText(user.getAge());
            sexText.setText(user.getSex());

            return view;
        }
    }

    class User {
        private int mAge;
        private String mName;
        private String mSex;

        public User(int age, String name, String sex) {
            this.mAge = age;
            this.mName = name;
            this.mSex = sex;
        }

        public String getName() {
            return this.mName;
        }

        public String getAge() {
            return this.mAge + "";
        }

        public String getSex() {
            return this.mSex;
        }
    }

     這里自定義了一個(gè)ArrayAdapter,有關(guān)于Adapter的使用在之前的SimpleAdapter中已經(jīng)涉及到了,所以這里直接就是以自定義的ArrayAdapter作為例子。
我們這里需要將學(xué)生的信息羅列出來(lái),需要三個(gè)TextView:

<?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" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

      

     在自定義ArrayAdapter的時(shí)候,最神奇的地方就是我們可以指定ArrayAdapter綁定的數(shù)據(jù)類型,可以是基本數(shù)據(jù)類型,也可以是自定義的對(duì)象類型,像是這次的User類型。對(duì)于自定義的ArrayAdapter的構(gòu)造方法,存在很多形式,這次是傳進(jìn)一個(gè)View的資源Id,但是我們也可以指定綁定的數(shù)據(jù)類型。
ArrayAdapter的神奇之處就是我們竟然可以像是操作Array一樣來(lái)操作ArrayAdapter!像是例子中的添加操作,而其他的適配器都是需要傳進(jìn)一個(gè)容器的。ArrayAdapter為什么可以處理對(duì)象類型的數(shù)據(jù)呢?其實(shí),ArrayAdapter是使用數(shù)組中對(duì)象的toString()方法來(lái)填充指定的TextView,所以我們可以通過(guò)重寫(xiě)對(duì)象的toString()方法來(lái)自定義ListView的顯示。

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            User user = getItem(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(mResourceId, null);

            TextView text = (TextView) view.findViewById(R.id.info);
            text.setText(user.toString());
            return view;
        }

      class User {
          private int mAge;
          private String mName;
          private String mSex;

          public User(int age, String name, String sex) {
             this.mAge = age;
             this.mName = name;
             this.mSex = sex;
         }

        @Override
        public String toString() {
            return "姓名:" + mName + " " + "年齡:" + mAge + " " + "性別:" + mSex;
        }
    }

     這樣我們可以只在一行中顯示所有數(shù)據(jù)。

    

     使用ArrayAdapter最大的疑問(wèn)就是我們是否需要將一個(gè)現(xiàn)成的容器傳入ArrayAdapter中?原本ArrayAdapter本身就用一般容器的基本操作,像是添加新的元素等,但它本身并不能完成當(dāng)成容器使用,我們更多的時(shí)候是要將一個(gè)容器中的元素交給ArrayAdapter,由后者決定它的顯示形式。

class UserAdapter extends ArrayAdapter<User> {
        private int mResourceId;

        public UserAdapter(Context context, int textViewResourceId,
                List<User> users) {
            super(context, textViewResourceId, users);
            this.mResourceId = textViewResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            User user = getItem(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(mResourceId, null);

            TextView text = (TextView) view.findViewById(R.id.info);
            text.setText(user.toString());
            return view;
        }
    }
List<User> users = new ArrayList<User>();
users.add(new User(10, "小智", "男"));
users.add(new User(10, "小霞", "女"));
UserAdapter adapter = new UserAdapter(this, R.layout.list_item, users);
listView.setAdapter(adapter);

       如果我們將ArrayAdapter綁定的數(shù)據(jù)類型定義為Object,我們可以自由的傳入任何類型的容器而不需要任何有關(guān)類型轉(zhuǎn)換的操作!

       ArrayAdapter不僅僅是可以顯示TextView,它當(dāng)讓也像是其他Adapter一樣,可以顯示任何其他非TextView的組件:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) this.findViewById(R.id.list);
        List<Object> users = new ArrayList<Object>();
        users.add(10);
        users.add(11);
        UserAdapter adapter = new UserAdapter(this, R.layout.list_item,
                R.id.info, users);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class UserAdapter extends ArrayAdapter<Object> {
        private int mResourceId;

        public UserAdapter(Context context, int resourceId,
                int textViewResourceId, List<Object> users) {
            super(context, resourceId, textViewResourceId, users);
            this.mResourceId = resourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Object user = getItem(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(mResourceId, null);

            TextView text = (TextView) view.findViewById(R.id.info);
            text.setText(user.toString());
            return view;
        }
    }
<?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" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點(diǎn)擊" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

    

     如果我們的布局中需要其他組件,必須指定該布局中用于顯示ArrayAdapter中數(shù)據(jù)的TextView的Id。

     如果只是方便綁定數(shù)據(jù)的話,其實(shí)是沒(méi)有必要專門(mén)獨(dú)立個(gè)ArrayAdapter出來(lái),只要覆寫(xiě)getView()就可以,正如使用容器就是為了方便大量數(shù)據(jù)的處理一樣的道理,使用ArrayAdapter也是為了處理數(shù)據(jù)較大的情況,像是超過(guò)100條或者頻繁動(dòng)態(tài)增刪數(shù)據(jù)時(shí),就可以使用ArrayAdapter,而且,為了方便我們刷新UI,ArrayAdapter也提供了setNotifyOnChange()方法,這樣可以降低UI的處理量,使得刷新UI更加快速,主要是通過(guò)停止對(duì)add,insert,remove和clear的操作來(lái)實(shí)現(xiàn)這點(diǎn)。

  • Java中抽象類和接口的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java中抽象類和接口的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    java抽象類和接口最本質(zhì)的區(qū)別是接口里不能實(shí)現(xiàn)方法--接口中的方法全是抽象方法。抽象類中可實(shí)現(xiàn)方法--抽象類中的方法可以不是抽象方法,下文給大家簡(jiǎn)單介紹下,需要的的朋友參考下
    2017-04-04
  • Spring bean不被GC的真正原因及分析

    Spring bean不被GC的真正原因及分析

    這篇文章主要介紹了Spring bean不被GC的真正原因及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Spring boot實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離的方法

    Spring boot實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離的方法

    本篇文章主要介紹了Spring boot實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Java?BitMap源碼仿寫(xiě)實(shí)現(xiàn)

    Java?BitMap源碼仿寫(xiě)實(shí)現(xiàn)

    這篇文章主要介紹了Java?BitMap源碼仿寫(xiě)實(shí)現(xiàn),所謂bitmap,就是用每一位來(lái)存放某種狀態(tài),適用于大規(guī)模數(shù)據(jù),但數(shù)據(jù)狀態(tài)又不是很多的情況。通常是用來(lái)判斷某個(gè)數(shù)據(jù)存不存在的
    2022-12-12
  • Java 圖片復(fù)制功能實(shí)現(xiàn)過(guò)程解析

    Java 圖片復(fù)制功能實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了Java 圖片復(fù)制功能實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • java 自定義可繼承枚舉Enum的案例

    java 自定義可繼承枚舉Enum的案例

    這篇文章主要介紹了java 自定義可繼承枚舉Enum的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • SpringBoot @Retryable注解方式

    SpringBoot @Retryable注解方式

    這篇文章主要介紹了SpringBoot @Retryable注解方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • java中實(shí)現(xiàn)excel合并單元格詳細(xì)代碼實(shí)例

    java中實(shí)現(xiàn)excel合并單元格詳細(xì)代碼實(shí)例

    最近的工作中,遇到一個(gè)需求在生成的Excel表格后需要在尾部添加一個(gè)合并的單元格數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)excel合并單元格的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • MyBatis-Plus模糊查詢特殊字符串轉(zhuǎn)義的實(shí)現(xiàn)

    MyBatis-Plus模糊查詢特殊字符串轉(zhuǎn)義的實(shí)現(xiàn)

    使用MyBatis中的模糊查詢時(shí),當(dāng)查詢關(guān)鍵字中包括有_、\、%時(shí),查詢關(guān)鍵字失效,本文主要介紹了MyBatis-Plus模糊查詢特殊字符串轉(zhuǎn)義的實(shí)現(xiàn),感興趣的可以了解一下
    2024-06-06
  • 最新評(píng)論

    广安市| 定日县| 大厂| 武陟县| 新余市| 通城县| 望谟县| 金湖县| 张北县| 澄城县| 三门峡市| 长宁区| 宁强县| 祁东县| 珲春市| 萨嘎县| 宿州市| 清苑县| 宜春市| 肇庆市| 瑞昌市| 丹棱县| 新竹市| 句容市| 金门县| 孝感市| 星座| 新巴尔虎右旗| 迁安市| 贵定县| 刚察县| 海林市| 龙游县| 威海市| 苍南县| 宝山区| 乌拉特前旗| 迁安市| 芮城县| 凉山| 新平|