天天看點

Android ListView + ArrayAdapter、SimpleAdapter、BaseAdapter實作清單

效果圖:

ListView 與 ArrayAdapter 之間的配合:

Android ListView + ArrayAdapter、SimpleAdapter、BaseAdapter實作清單

activity_main.xml

<?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"
android:orientation="vertical"
tools:context=".MainActivity">

    <ListView
        android:id="@+id/ls_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>
           

MainActivity.java

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView  = findViewById(R.id.ls_view);
        //  創造資料源
        String[]  data = {"冰激淩","奶茶","芒果","西瓜","蕃茄炒蛋","剁椒魚頭","雞翅","冰激淩","奶茶","芒果","西瓜","蕃茄炒蛋","剁椒魚頭","雞翅","冰激淩","奶茶","芒果","西瓜","蕃茄炒蛋","剁椒魚頭","雞翅"};
        /**
         *      建立ArrayAdapter 擴充卡      —   把資料源放入擴充卡中
         *      第一個參數是:     context
         *      第二個參數是:     每一條資料顯示的布局item,這裡用系統預設的就行
         *      第三個參數是:     放入剛剛建立好的資料源
         */
        ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);

        listView.setAdapter(adapter);       //  再讓清單與擴充卡産生聯系

    }

}
           

ListView 與 SimpleAdapter 之間的配合:

Android ListView + ArrayAdapter、SimpleAdapter、BaseAdapter實作清單

activity_main.xml:

<?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"
android:orientation="horizontal"
tools:context=".MainActivity">
    
    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
    
</LinearLayout>
           

ly_item.xml:

<?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"
    android:orientation="horizontal"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/img_view"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="40dp"
        android:textSize="25dp"
        android:text="你好"
        />

    <TextView
        android:id="@+id/tv_info"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="40dp"
        android:textSize="25dp"
        android:text="你好"></TextView>

    </LinearLayout>


</LinearLayout>
           

MainActivity.java:

public class MainActivity extends AppCompatActivity  {
    private String[] name = {"狐狸","玫瑰","國王"};                       //  名字
    private String[] info = {"聰明的狐狸","帶刺的花兒","沒有一個臣民"};     //  介紹
    private int[] image = {R.mipmap.ic_one,R.mipmap.ic_two,R.mipmap.ic_three};      //  每一個item對應的圖示
    List<Map<String,Object>> list = new ArrayList<>();      //  建立 資料源 list

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //  給資料源裝配資料
        for (int i=0;i<name.length;i++){                
            Map<String,Object> map = new HashMap<>();       //  在建立一個map集合
            map.put("image",image[i]);
            map.put("name",name[i]);
            map.put("info",info[i]);
            list.add(map);              //  将每一組map對象的值,加入到list集合中
        }

        /**
         *      建立 SimpleAdapter 擴充卡
         *      第一個參數: context
         *      第二個參數: 放入資料源
         *      第三個參數: 每一個item的布局
         *      第四個參數: 建立map的鍵對象,并放入相應的值,因為要從資料源中識别對應值,每一個鍵對象對應着一個值對象
         *      第五個參數: 建立一個int對象,放入item布局中 每一個控件對應的id,這個id與剛才的鍵對象位置要對應
         */
        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,list,R.layout.ly_item,new String[]{"image","name","info"},new int[]{R.id.img_view,R.id.tv_name,R.id.tv_info});

        //  得到listView 對象
        ListView listView = findViewById(R.id.lv_list);
        
        //  讓listview 與 SimpleAdapter 擴充卡産生聯系
        listView.setAdapter(adapter);
        
    }
    
}
           

ListView 與 BaseAdapter 之間的配合【推薦】:

Android ListView + ArrayAdapter、SimpleAdapter、BaseAdapter實作清單

activity_main.xml:

<?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"
android:orientation="horizontal"
tools:context=".MainActivity">

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

</LinearLayout>
           

ly_item.xml:

<?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"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/img_view"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="vertical">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="40dp"
        android:textSize="25dp"
        android:text="你好"
        />

    <TextView
        android:id="@+id/tv_info"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="40dp"
        android:textSize="25dp"
        android:text="你好"></TextView>

    </LinearLayout>
    
</LinearLayout>
           

MainActivity.java:

public class MainActivity extends AppCompatActivity  {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = findViewById(R.id.lv_list);
        List<CostBean> costBeanList = new ArrayList<>();	//建立封裝好的bean對象

		//	裝配資料源
        costBeanList.add(new CostBean(R.mipmap.ic_one,"狐狸","聰明的狐狸"));
        costBeanList.add(new CostBean(R.mipmap.ic_two,"玫瑰","帶刺的花兒"));
        costBeanList.add(new CostBean(R.mipmap.ic_three,"國王","沒有一個臣民"));

		//	因為 MyAdapter 繼承了 BaseAdapter,是以這裡直接讓 listView 與 Myadapter産生聯系
        listView.setAdapter(new MyAdapter(this,costBeanList));	
    }

}
           

CostBean.java:

**
 *      CostBean ——  封裝資料源
 */

public class CostBean {

    private int imgId;
    private String name;
    private String info;

    public CostBean(int imgId, String name, String info) {
        this.imgId = imgId;
        this.name = name;
        this.info = info;
    }

    public int getImgId() {
        return imgId;
    }


    public String getName() {
        return name;
    }

           

MyAdapter.java:

**
 *      BaseAdapter ——  擴充卡管理資料源
 */

public class MyAdapter extends BaseAdapter {
    private List<CostBean> costBeanList;
    private LayoutInflater inflater;

    //  通過構造方法關聯資料源與擴充卡
    public MyAdapter(Context context,List<CostBean> costBeanList) {
        this.costBeanList = costBeanList;

        //  context:要使用目前的Adapter的界面對象mInflater:布局 裝載器對象
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return costBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return costBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //第一次建立view即可,以免給程式帶來太多負擔
        if (convertView == null){
            holder = new ViewHolder();
            //            由于我們隻需要将XML轉化為View,并不涉及到具體的布局,是以第二個參數通常設定為null
            convertView = inflater.inflate(R.layout.ly_item,null);
            holder.imageView = convertView.findViewById(R.id.img_view);
            holder.title = convertView.findViewById(R.id.tv_name);
            holder.content = convertView.findViewById(R.id.tv_info);
            //這句話的意思就是,讓convertView與holder産生關聯,通過set儲存在Tag中
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
            //通過getTag,取出上面綁定的holder,以免多次綁定,造成資源浪費
        }

        //設定控件的資料,可以了解為從封裝的資料中取出一個一個對應的值
        CostBean costBean = costBeanList.get(position);
        holder.imageView.setImageResource(costBean.getImgId());
        holder.title.setText(costBean.getName());
        holder.content.setText(costBean.getInfo());
        return convertView;
    }

    class ViewHolder{
        public ImageView imageView;
        public TextView title;
        public TextView content;
    }
}