1.AdapteView

AdapteView 继承ViewGroup它的本质是容器

 

AdapterView派生了3个子类:

AbsListView

AbsSpinner

AdapterViewAnimation

这3个类是抽象类

实际使用中采用的是它们的子类

 

 安卓第六天笔记--ListView

2.Adpate结构

 安卓第六天笔记--ListView

 

3.使用ArrayAdapter完成ListView显示

 

 安卓第六天笔记--ListView

ListView使用的是ListAdapter类型的适配器

由于只显示文字,所有使用ArrayAdapter

ArrayAdapter 是ListAdapter的子类

 

布局文件

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

<!--

divider:分隔线颜色

dividerHeight:分隔线高度

ListView

建议 layout_width:与 layout_height 都设置为match_parent

要预览的话要有ID属性

-->

<ListView

    android:id="@+id/lv"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:divider="@color/colorAccent"

    android:dividerHeight="2dp">



</ListView>



</LinearLayout>

 

 

Activity

/**

 * ListView使用

 * ArrayAdapter适配器

 */

public class MainActivity extends AppCompatActivity {



    /*

    ListView

     */

    private ListView lv;



    private String [] items={

            "孙悟空",

            "猪八戒",

            "牛魔王",

            "JAVA",

            "Android",

            "Ajax",

            "XML",

            "Spring",

            "Hibernate",

            "Spring MVC",

            "MyBatis"

    };



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        lv = (ListView) findViewById(R.id.lv);



        /*

        public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull T[] objects)



        context:上下文

         */

        //为ListView设置适配器

        lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,items));





    }

}

 

 

 

android.R.layout.simple_list_item_1

内部就是一个TextView

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@android:id/text1"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:textAppearance="?android:attr/textAppearanceListItemSmall"

    android:gravity="center_vertical"

    android:paddingStart="?android:attr/listPreferredItemPaddingStart"

    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"

    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

 

 

4.使用SimpleAdapter

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
        @LayoutRes int resource, String[] from, @IdRes int[] to)

 

 

context:上下文

LIst<? extends Map<String,/.>> data: 集合类型的对象,每个元素都是一个Map<String,?>对象

resource:提定一个界面布局的ID,布局文件

from:该参数提取Map<String,?>的key

to:int[]类型的参数决定填充哪些控件

 安卓第六天笔记--ListView

 

 

布局文件:

<?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:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.itheima.listviewsimpleadapter.MainActivity">





    <!--

    ListView

    -->

    <ListView

        android:id="@+id/lv"

        android:layout_width="match_parent"

        android:layout_height="match_parent">



    </ListView>



</RelativeLayout>
 
View Code

相关文章: