Android BaseAdapter用法

來源:互聯網
上載者:User

標籤:tool   pre   null   object   man   com   alt   new   執行個體化   

BaseAdapter 適配器

BaseAdapter是一個抽象類別,因此要寫自已的適配器,段繼承此類,並實現以下方法:
@Override
public int getCount() { return 0;}
@Override

public Object getItem(int position) { return null;}
@Override

public long getItemId(int position) { return 0;}
@Override
public View getView(int position, View convertView, ViewGroup parent) { return null;}

Demo:

描述:用ListView,BaseAdapter實現多個使用者資訊展示,根據使用者性不同顯示不同的性別圖片(如)

代碼如下:

主介面布局檔案:activity_main<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.zy.baseadapter.MainActivity">    <ListView        android:id="@+id/user_list_view"        android:layout_width="match_parent"        android:layout_height="match_parent">    </ListView></RelativeLayout>

 

//item_user.xml 布局檔案<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:scaleType="centerInside"        android:id="@+id/iv_user_img"        android:layout_width="60dp"        android:layout_height="60dp" />    <TextView        android:id="@+id/tv_user_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv_user_age"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv_user_sex"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

圖片檔案

 

 

public class MainActivity extends AppCompatActivity {    List<UserInfo> userInfos = new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);     //使用者資料        for (int i = 0;i < 30;i++){            UserInfo userInfo = new UserInfo();            userInfo.userName = "張" + i;            userInfo.userAge = i;            userInfo.userSex = i%2 == 0?"男":"女";            userInfos.add(userInfo);        }     //listView        ListView listView = (ListView) findViewById(R.id.user_list_view);     //執行個體化一個適配器        UserInfoListAdapter userInfoListAdapter = new UserInfoListAdapter();        listView.setAdapter(userInfoListAdapter);    }//    點擊螢幕跳轉到第二個Activity (點擊事件)//    @Override//    public boolean onTouchEvent(MotionEvent event) {//        int action = event.getAction();//        if(action == MotionEvent.ACTION_DOWN){//            Intent intent = new Intent(this,MenuActiviy.class);//            startActivity(intent);//        }//        return false;//    }    class UserInfoListAdapter extends BaseAdapter{        @Override        public int getCount() {            return userInfos.size();        }        @Override        public Object getItem(int position) {            return userInfos.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            View itemRootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user,null);            TextView userNameView = (TextView) itemRootView.findViewById(R.id.tv_user_name);            userNameView.setText(userInfos.get(position).userName);            TextView userAgeView = (TextView) itemRootView.findViewById(R.id.tv_user_age);            userAgeView.setText(userInfos.get(position).userAge + "");            TextView userSexView = (TextView) itemRootView.findViewById(R.id.tv_user_sex);            userSexView.setText(userInfos.get(position).userSex);            ImageView imageView = (ImageView) itemRootView.findViewById(R.id.iv_user_img);            if(userInfos.get(position).userSex.equals("男")){                imageView.setBackgroundResource(R.drawable.man);            }else if(userInfos.get(position).userSex.equals("女")){                imageView.setBackgroundResource(R.drawable.woman);            }            return itemRootView;        }    }    class UserInfo{        private String userName;        private int userAge;        private String userSex;    }}

 

Android BaseAdapter用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.