Android——列表视图(ListView)

列表视图是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();
	}

}

  

时间: 2024-08-27 07:41:55

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

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

一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layou

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

ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivity,然后使用setListAdapter()创建,如果需要在xml中创建,需要android:id = @id:android:list Listview和GridView和Spinner和Gallery等Adapterview都是容器,用adapter来提供数据,而adapterView负责数据

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

activity_activitybase.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_pa

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

SimpleAdapter:可显示文字加图片 activity_activitysimple.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android

列表视图ListView之一

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示. 一.简单应用 1.打开"res/layout/activity_main.xml"文件. (1)从工具栏向activity拖出1个列表视图ListView. (2)打开activity_main.xml文件. 完整代码如下: <?xml version="1.0" encoding="utf-8"?><Rela

Android学习-列表视图ListView

一.简介: ListView,列表视图,直接继承了AbsListView,是一个以垂直方式在项目中显示View视图的列表.ListView的数据项,来自一个继承了ListAdapter接口的适配器. 二.新建一个包listview并新建ListViewActivity.java活动: 12345678 public class ListViewActivity extends AppCompatActivity { @Override protected void onCreate(Bundle

列表视图ListView之二

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示. 在上一章,我们采用ArrayAdapter填充ListView,本章我们了解一下SimpleAdapter的应用. 一.SimpleAdapter应用 1.打开"res/layout/activity_main.xml"文件. 完整代码如下: <?xml version="1.0" encoding="utf-8"?>

高级控件【安卓5】——滚动视图、列表视图[ListView、SimpleAdapter类]

滚动视图 <ScrollView android: layout_width="fill_parent" android: layout_height="fill_parent"> </ScrollView> 滚动试图指的是提供一个专门的容器,可以装下多于屏幕宽度的组件,而后采用拖拽的方式 显示所有在ScrollView中的组件 注意:滚动视图中只能有一个组件 列表视图[ListView.SimpleAdapter类] 1.ListView控

andorid 列表视图 ListView 之ArrayAdapter

activity_ui3.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_p