其他分享
首页 > 其他分享> > BaseAdapter(安卓开发学习笔记————17)

BaseAdapter(安卓开发学习笔记————17)

作者:互联网

1.它的方法包括:

请添加图片描述

2.代码示例:
(1)java代码:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<UserInfo> dataList;  //数据源

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView demoListView = (ListView)findViewById(R.id.demo_list_view);

        dataList = new ArrayList<>();

        for (int i=0; i<50; i++){
            UserInfo userInfo = new UserInfo();
            userInfo.name = "张三"+i;
            userInfo.age = 10+i;
            userInfo.sex = i%2==0?"男":"女";
            dataList.add(userInfo);

        }

        UserInfoListAdapter userInfoListAdapter = new UserInfoListAdapter();
        demoListView.setAdapter(userInfoListAdapter);

    }

    class UserInfoListAdapter extends BaseAdapter{      /**BaseAdapter:适用于较为复杂的界面**/

           @Override
           public int getCount() {          //返回多少个数据显示
               return dataList.size();
           }

           @Override
           public Object getItem(int position){
               return dataList.get(position);       /**返回指定数据源**/

           }

           @Override
            public long getItemId(int position){
               return  position;
                        //返回指定数据源的ID

           }

           //返回指定position的每个item的布局文件的view,这个view 将会显示在指定position的位置
            @Override
             public View getView(int position, View convertView, ViewGroup parent){

                                                                            /**LayoutInflater.from(parent.getContext()).inflate()???????????**/
               View  itemRootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_userinfo,null);

                TextView tvName = (TextView)  itemRootView.findViewById(R.id.tv_name);
                tvName.setText(dataList.get(position).name);    //此处的get(position)的到的是UserInfo的实例

                TextView tvAge = (TextView) itemRootView.findViewById(R.id.tv_age);
                tvAge.setText(dataList.get(position).age+"");      /**setText()---支持字符型的id,需要变为字符串类型**/

                TextView tvSex = (TextView) itemRootView.findViewById(R.id.tv_sex);
                tvSex.setText(dataList.get(position).sex);

                ImageView imageViewSex =(ImageView)itemRootView.findViewById(R.id.img_view_sex);
                if (dataList.get(position).sex.equals("男")){
                   imageViewSex.setBackgroundResource(R.drawable.huge);

                }else if (dataList.get(position).sex.equals("女")){
                    imageViewSex.setBackgroundResource(R.drawable.gao);

                }
               return itemRootView;

            }
    }

    class UserInfo{

        String name;
        int age;
        String sex;

    }

}

(2)布局代码:
第一部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/demo_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>

第二部分:(新建一个名为:item_userinfo的布局文件)

<?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:id="@+id/img_view_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ImageView>

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

    </TextView>

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

    </TextView>

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

    </TextView>

</LinearLayout>

标签:17,BaseAdapter,dataList,安卓,get,import,position,android,TextView
来源: https://blog.csdn.net/qq_47004770/article/details/122017654