ListView基本示例

ListView在Android中应用十分广泛,如新闻列表、电话号码列表 、联系人列表等等。它一般垂直显示在手同屏幕上,可以自上而下地拉动。一般使用Adapter方法向列表中填充数据,数据来源有很多,如数组、数据库等等。


ListView属性(摘自google官方)

XML Attributes
Attribute Name Related Method Description
android:divider   Drawable or color to draw between list items. 
android:divider setDivider(Drawable) Drawable or color to draw between list items. 
android:dividerHeight   Height of the divider. 
android:entries   Reference to an array resource that will populate the ListView. 
android:footerDividersEnabled   When set to false, the ListView will not draw the divider before each footer view. 
android:headerDividersEnabled   When set to false, the ListView will not draw the divider after each header view. 



示例


(效果图)


strings.xml

<resources>
    <string name="app_name">列表基本示例</string>
    <string name="action_settings">Settings</string>
</resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1cb7eb</color>
    <color name="colorPrimaryDark">#1cb7eb</color>
    <color name="colorAccent">#FF4081</color>
</resources>

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

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

</LinearLayout>

layout/activity_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:height="60dp">
</TextView>

mainactivity.java

package com.my.asus.listviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    // Array of strings...
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X","Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};



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

        ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_listview, mobileArray);
        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}





代码分析

ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_listview, mobileArray);

 数据源是数组(array)的时候,可以使用adapter。以上表达式的意思是将自定义的数组mobileArray中的项逐个填充到activity_listview.xml中的TextView中。 

第一个参数this:一般都用this,指这个应用。

第二个参数:在layout中创建的xml,其中包含用来接受数组项的TextView。

第三个参数:自定义的数据名称。






源码下载






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值