列表视图是android中最常用的一种视图组件,它以垂直列表的形式列出需要显示的列表项。在android中有两种方法向屏幕中添加列表视图:一种是直接使用ListView组件创建;另外一种是让Activity继承ListActivity实现。下面分别介绍这两种方法:

一、直接使用ListView组件创建

在布局文件中首先添加ListView

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/cytpe" > </ListView> </LinearLayout>

这里使用了名称为ctype的数组资源,因此我们要在res/values目录中创建一个定义的数组资源的xml文件arrays.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string-array name="cytpe">
<item>情景模式</item>
<item>主题模式</item>
<item>手机</item>
<item>程序管理</item>
<item>通话设置</item>
<item>连接功能</item>
</string-array> </resources>

直接运行就可以看到如下所示的列表视图:

下面通过适配器来指定列表项来创建ListView

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/ic_launcher"
android:dividerHeight="3px"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
> </ListView> </LinearLayout>

Java代码:

package com.basillee.blogdemo;

import java.lang.annotation.Retention;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.listView1);
ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this,R.array.cytpe,android.R.layout.simple_list_item_single_choice);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String result=parent.getItemAtPosition(pos).toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
});
} }

二、让Activity继承ListActivity实现:

如果程序的窗口仅仅需要显示一个列表,则可以直接让Activity继承ListActivity来实现。继承ListActivity的类中无需调用setContentView方法来显示页面,而是可以直接为其设置适配器,从而显示一个列表。

废话少说直接看看代码大家就都懂了:

package com.basillee.blogdemo;

import java.lang.annotation.Retention;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends ListActivity{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String []cytpeStrings=new String[]{"情景模式","主题模式","手机","程序管理"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,cytpeStrings);
setListAdapter(adapter);
} @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String resultString=l.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), resultString, Toast.LENGTH_LONG).show();
} }

  

Android——列表视图(ListView)的更多相关文章

  1. Android列表视图ListView和ListActivity-android学习之旅(二十四)

    ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...

  2. Android——列表视图 ListView(一)Arrayadapter

    一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf ...

  3. Android——列表视图 ListView(三)BaseAdapter

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

  4. Android——列表视图 ListView(二)SimpleAdapter

    SimpleAdapter:可显示文字加图片 activity_activitysimple.xml <?xml version="1.0" encoding="u ...

  5. Android列表视图(List View)

    Android列表视图(ListView) ListView是一个显示滚动项列表的示视图组(viewgroup),通过使用适配器(Adapter)把这些列表项自动插入到列表中.适配器比如从一个数组或是 ...

  6. React-Native基础_5.列表视图ListView

    列表视图ListView 用来显示垂直滚动列表,需要指定两个东西,1 数据的来源 dataSource,2 渲染列表的条目布局 rendRow 'use strict' import React, { ...

  7. Android 自学之列表视图ListView和ListActivity

    ListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: 直接使用ListView创建. 让Activity继承ListActivity ...

  8. 滚动视图、列表视图[ListView、SimpleAdapter类]

    滚动视图 <ScrollView android: layout_width="fill_parent" android: layout_height="fill_ ...

  9. 列表视图(ListView和ListActivity)

    在ListView中显示网络图片 ImageView 类虽然有一个 setImageUri 方法,但不能直接接受一个由网络地址生成的uri作为参数从而显示图片,我们只好使用其 setImageBitm ...

随机推荐

  1. leetcode@ [139/140] Word Break & Word Break II

    https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, determine ...

  2. 字符流缓冲区BufferedReader之readLine方法的原理

  3. 转载 在.net中使用GAC

    转载出处 https://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的d ...

  4. (莱昂氏unix源代码分析导读-50)LP11行式打印机

    by cszhao1980 LP11有两个设备寄存器:状态寄存器(lpsr)和数据缓冲寄存器(lpbuf),可通过以下结构进行访问: 8812: #define LPADDR 0177514 8823 ...

  5. IDF实验室-简单的ELF逆向 writeup

    题目:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=39 下载得到ElfCrackMe1文件,直接用IDA打开 ...

  6. cocos2d_随手篇1_关于ccTouchBegan的调用

    在新的cocos框架里,旧的调用ccTouchBegan方法被和谐掉了!so! 直接来代码 1 -(void)doSometing{ 2   [[[CCDirector sharedDirector] ...

  7. 64位Ubuntu配置android环境报错(...adb": error=2, 没有那个文件或目录)

    Failed to get the adb version: Cannot run program "/home/jayhomzhou/android/android-sdk/platfor ...

  8. Windows Server 2012网卡Teaming模式

    成组模式: Switch-independent(交换机独立): 这是配置时的默认值,此模式不要求交换机参与组合配置,由于独立模式下的交换机不知道网卡是主机上组合一部分,teaming组中的网卡可以连 ...

  9. 基础数据结构 之 树(python实现)

    树是数据结构中常用到的一种结构,其实现较栈和队稍为复杂一些.若树中的所有节点的孩子节点数量不超过2个,则该为一个二叉树.二叉树可用于查找和排序等.二叉树的主要操作有:建树,遍历等.遍历是树中的一个最为 ...

  10. 【转】使用junit进行单元测试(中级篇)

    转自:http://blog.csdn.net/andycpp/article/details/1327346 我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架, ...