展会信息港展会大全

Android之UI学习篇:ListView控件学习(一)
来源:互联网   发布日期:2015-11-26 16:18:42   浏览:2375次  

导读:ListView这个控件使用的非常普遍,关于它的基本介绍,我们来看一下API中的介绍:Class OverviewA view that shows items in a vertical...

ListView这个控件使用的非常普遍,关于它的基本介绍,我们来看一下API中的介绍:

Class Overview

A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.

我们的LIstView中显示的数据都是通过ListAdapter这个接口来配置的,通常是用它的某一个子类来配置数据,下面来介绍一下各种适配器以及他们的关系:

ListAdapter

implements Adapter

android.widget.ListAdapter

Known Indirect Subclasses

ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, WrapperListAdapter

Class Overview

Extended Adapter that is the bridge between a ListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter.

在ListAdapter的众多子类当中,用的最多的就是ArrayAdapter(存储数组)、SimpleAdapter(字符串)、CursorAdapter(数据库中的数据)。

在使用LIstView控件来显示数据时,有两种实现方式:

第一种实现方式: 在xml中定义<LIstView>布局对象,设置它的相关属性,然后在Activity中对它进行配置和事件监听;

第二种实现方式: 让你的 Activity 类继承 ListActivity,可以通过getListView()来获取,可以不写ListView的xml文件。

本篇文章将以第二种方式为例来做一个简单的实例。

首先来认识一下ListActivity这个类:

java.lang.Object

android.content.Context

android.content.ContextWrapper

android.view.ContextThemeWrapper

android.app.Activity

android.app.ListActivity

正如名字含义,它是Activity的一个子类。

Class Overview

An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen

我可以直接使用内置的默认, full-screen list,当然很多时候要做一些扩充,我们可以自定义screen layout,使用setContentView() in onCreate()来显示定义的ListView, 不过这里有一要求,To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)

这样的原因是什么呢?

原因是我们继承ListActivity这个类,使用getListView()来取得它,系统在执行该方法的时候是按照 id= list 去查找,所以我们必须定义Id为" @android:id/list"。知道了吧!

我们通过一个在String.xml文件里面配置多个国家的名称,然后通过适配器ArrayAdapter来显示这些数据到我们的LIstView控件中,并且提供根据用户输入的匹配符,筛选出匹配的国家名称。

运行结果:

大家可以看到国家的名称已经显示在LIstView控件当中了,当我们输入"be"是自动匹配的结果截图:

其实这个自动匹配功能很简单,只要在代码中调用这句代码:listView.setTextFilterEnabled(true);

工程目录结构:

以下是源代码:

MainActivity.java:

[html]

package com.listview.activity;

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends ListActivity implements OnItemClickListener {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

String[] countries = getResources().getStringArray(R.array.countries_array);

// 为ListView设置适配器

setListAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, countries));

ListView listView = getListView();

//设置Item的选中事件监听器

listView.setOnItemClickListener(this);

//这个属性为true表示listview获得当前焦点的时候,相应用户输入的匹配符,筛选出匹配的listview Items

listView.setTextFilterEnabled(true);

}

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(MainActivity.this, ((TextView)view).getText(), 1).show();

}

}

main.xml在这里没起作用,这里就不给出代码了。

list_item.xml:

[html]

<?xml version="1.0" encoding="utf-8"?>

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

android:textSize="16sp" >

</TextView>

strings.xml:

[html]

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, MainActivity!</string>

<string name="app_name">HelloListView</string>

<string-array name="countries_array">

<item>Bahrain</item>

<item>Bangladesh</item>

<item>Barbados</item>

<item>Belarus</item>

<item>Belgium</item>

<item>Belize</item>

<item>Benin</item>

<item>Bermuda</item>

<item>Bhutan</item>

<item>Bolivia</item>

<item>Bosnia and Herzegovina</item>

<item>Botswana</item>

<item>"Bouvet Island</item>

<item>Brazil</item>

</string-array>

</resources>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港