Android Studio第13课——Android当中的ListView

ListView的功能:
1.将数据填充到布局;
2.处理用户的选择点击等操作。

列表的显示需要三个元素:
1.ListVeiw:用来展示列表的View;
2.适配器: 用来把数据映射到ListView上的中介;
3.数据源: 具体的将被映射的字符串,图片,或者基本组件。

关于适配器
适配器是一个连接数据和AdapterView的桥梁,通过它能有效地实现数据与AdapterView的分离设置,
使AdapterView与数据的绑定更加简便,修改更加方便。将数据源的数据适配到ListView中的常用适配器有:
ArrayAdapter、SimpleAdapter 和 SimpleCursorAdapter。


简单做了一个app测试,先上图:



点击事件:


最后贴上源码:

user.xml部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/user_name"
        android:layout_width="180dp"
        android:layout_height="30dp"
        android:layout_gravity="left"
        android:textSize="10pt"
        android:singleLine="true"
        />
    <!--gravity属性是用来设置文本的对齐方式-->
    <TextView
        android:id="@+id/user_id"
        android:layout_width="180dp"
        android:layout_height="30dp"
        android:gravity="right"
        android:textSize="10pt"
        />

</LinearLayout>

main.xml部分:

<?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="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!--
        这里的ListView使用的id是Android内置的list ID
        这个ID不加会出现闪退的现象

        scrollbars是在条目很多的情况下,屏幕滚动条滑动显示更多内容的方向

        android:drawSelectorOnTop="true"
        点击某一条记录,颜色会显示在最上面,记录上的文字被遮住,
        所以点击文字不放,文字就看不到

        android:drawSelectorOnTop="false"
        点击某条记录不放,颜色会在记录的后面,成为背景色,
        但是记录内容的文字是可见的-->
        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            android:scrollbars="vertical">

        </ListView>
    </LinearLayout>


</LinearLayout>

Activity.java部分:

package com.example.urien.handler;

import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;



//ListAcrivity是Activity的子类,是专门为了实现列表形式的Activity
public class MainActivity extends ListActivity {

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

        //定义一个ArrayList类型的动态list ,里面存放的是HashMap键值对形式
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();


        for(int i=0;i<40;i++){
            //定义一个临时的hashMap
            HashMap<String,String> hashMap = new HashMap<String, String>();
            //存入姓名键值对
            hashMap.put("user_name","user_"+ i);
            //存入ID
            hashMap.put("user_id","100"+i);
            //讲hashMap存入List
            list.add(hashMap);
        }


    /**
     * 安卓当中很常见的一个SimpeAdapter 主要就是用来添加一些数据
     * Constructor
     *
     * @param context The context where the View associated with this SimpleAdapter is running
     * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The
     *        Maps contain the data for each row, and should include all the entries specified in
     *        "from"
     * @param resource Resource identifier of a view layout that defines the views for this list
     *        item. The layout file should include at least those named views defined in "to"
     * @param from A list of column names that will be added to the Map associated with each
     *        item.
     * @param to The views that should display column in the "from" parameter. These should all be
     *        TextViews. The first N views in this list are given the values of the first N columns
     *        in the from parameter.
     */

        SimpleAdapter listAdpter = new SimpleAdapter(
                this,
                list,
                R.layout.user,
                new String[] {"user_name","user_id"},
                new int[] {R.id.user_name,R.id.user_id}
                );
        /*这个方法是ListAcrivity里面继承过来的*/
        setListAdapter(listAdpter);
    }

    /**
     * List条目点击监听事件
     * */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        /**id和position从上至下从0开始*/
        System.out.print(", id--------- " + id);
        System.out.println(", position- " + position);
    }
}

注释都标的很清楚,不多赘述。

By Urien 2018年6月23日 11:55:02



  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值