ListActivity实现列表学习笔记
2015-07-30 15:07
309 查看
如果程序的窗口仅仅需要显示一个列表,则可以直接让Activity继承ListActivity来实现,ListActivity的子类无须调用setContentView()方法来显示某个界面,而是可以直接传入一个内容Adapter,ListActivity的子类就呈现出一个列表。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
java代码:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 无需使用布局文件
String[] arr = { "孙悟空", "猪八戒", "唐僧" };
// 创建ArrayAdapter对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, arr);
// 设置该窗口显示列表
setListAdapter(adapter);
}
}
显示效果:
ListActivity实现列表学习笔记
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
java代码:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 无需使用布局文件
String[] arr = { "孙悟空", "猪八戒", "唐僧" };
// 创建ArrayAdapter对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, arr);
// 设置该窗口显示列表
setListAdapter(adapter);
}
}
显示效果:
ListActivity实现列表学习笔记
相关文章推荐
- Can you solve this equation?
- nyoj35表达式求值
- 链表 C++描述
- eclipse的快捷键--收藏
- PARSEC测试集的应用领域
- oracle服务介绍
- 从escapeshellcmd bypass说起到宽字节注入
- 141.Linked List Cycle (判断一个单链表是否有环)
- NYOJ 68
- Android线程调度机制
- 通过wifi离线推广ios app
- 杭电1213 How Many Tables(并查集找根节点)
- SQL之CONVERT(varchar(100), GETDATE(), 0)
- MySQL的语句执行顺序
- fresh in werkzeug
- python eval的常见错误封装及利用原理
- Fibonacci的【兔子数列】 以及 【狐狸找兔子】的问题 及 算法!
- DHCP服务器 server 2003 到 server 2012 迁移 (二)
- python 安全编码&代码审计
- 如何让人的一生价值最大