zoukankan      html  css  js  c++  java
  • Android-ListView-SimpleAdapter

    我在上一篇博客中Android-动态添加控件到ScrollView,写到可以用Java动态添加控件到Scrollview的孩子LinearLayout里面去,这种方式是不合理的,因为这种方式是一次性把数据全部加载完成了

    在Android中就提供了专门列表显示条目的控件,ListView控件,ListView控件不是一次性加载全部数据,他是只加载用户在屏幕看得到的数据,当用户滑动的过程中在去加载新的数据,同时会自动销毁之前加载过的数据

    什么时候使用ListView ?

    答:当很多条目Item,显示都内容不同,数据不同,但是存放的位置是相似的,并且动态变化的条目Item(和数据),就可以用ListView

    ListView的使用:

    1.在布局中定义ListVIew

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- 定义ListView -->
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>
    
    </RelativeLayout>

    2.在Java代码中找到ListVIew,再设置适配器 setAdapter(ListAdapter的子类)


     适配器 ArrayAdapter和SimpleAdapter 对比介绍:

    ArrayAdapter适配器是数组适配器,只能去适配展示很简单的格局,只能展示条目Item的一个控件

    SimpleAdapter适配器是好用适配器,可以展示比ArrayAdapter复杂一些到格局,可以展示一个条目Item的多个控件

     ListAdapter是SimpleAdapter的父类:

    ListVIew + ArrayAdapter:

    package liudeli.ui.all;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class ListViewActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_listview);
    
            ListView listView = findViewById(R.id.listview);
    
            List<Map<String, String>> list = new ArrayList<>();
            for (int i=0; i< 60; i++) {
                Map<String, String> map = new HashMap<>();
                map.put("id", "编号" + i +1);
                map.put("name", "艺名" + i + 100);
                list.add(map);
            }
    
    
            // 使用SimpleAdapter适配器
            ListAdapter listAdapter =
                    new SimpleAdapter(this, // 上下文
                                      list,         // 数据
                                      android.R.layout.simple_list_item_2, // 使用系统的布局
                                      new String[]{"id", "name"}, // 设置Map的key,数据从哪里来
                                      new int[]{android.R.id.text1, android.R.id.text2}); // 系统布局的两个控件ID,数据设置到那里去
    
            // 把适配器给ListView
            listView.setAdapter(listAdapter);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    }

    展示:

  • 相关阅读:
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    团队作业-随堂小测(同学录)
    Alpha 冲刺 (5/10)
    LeetCode-1
    c++向量
    软件工程实践总结作业
    个人作业——软件产品案例分析
    个人技术博客Alpha----Android Studio学习
  • 原文地址:https://www.cnblogs.com/android-deli/p/10096509.html
Copyright © 2011-2022 走看看