Android ListActivity学习

    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.
ListActivity 以列表的形式显示绑定的数据,数据来自数组或者是Cursor,同时提供一些用户选择某个item的处理函数,ListActivity 拥有一个ListView对象,这个对象可以绑定不同的数据源,典型的就是数组或者Cursor。它本身继承于Activity,所以很多Activity中的方法它都能用。

新建一个工程,继承ListActivity

点击(此处)折叠或打开

  1. public class ListAyActivity extends ListActivity {
  2.     @Override
  3.     protected void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         List<Map<String, Object>> items = getDate();
  6.         setContentView(R.layout.activity_list_ay);

  7.         SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.listay,
  8.                 new String[] {"title" }, new int[] {android.R.id.text1 });
  9.         setListAdapter(adapter);
  10.     }
在onCreate 的时候,使用 SimpleAdapter绑定数据,根据需要,可以绑定不同的 Adapter ,这里使用 SimpleAdapter,在界面上显示文字,实现一个查询分享功能的列表,如微博 微信,当我们点击列表的时候,弹出分享界面。
SimpleAdapter构造函数:  public SimpleAdapter(Context context, List > data, int resource, String[] from, int[] to) , 构造函数的参数之间有一定的关系,data中的数据类型为Map >, Map >中的String 和 String[] from对应,是要显示的内容在代码中是 title 的value ,  int[] to 参数标示要显示到哪里去,代码中是显示到android.R.id.text1。看一下显示数据的构造:

点击(此处)折叠或打开

  1. private List<Map<String, Object>> getDate() {
  2.         List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();

  3.         Intent mainIntent = new Intent(Intent.ACTION_SEND, null);
  4.         mainIntent.setType("text/plain");
  5.         mainIntent.putExtra(Intent.EXTRA_SUBJECT, "Shared");

  6.         PackageManager pm = getPackageManager();
  7.         List<ResolveInfo> lists = pm.queryIntentActivities(mainIntent, 0);

  8.         for (ResolveInfo list : lists) {
  9.             Intent intent = new Intent().setClassName(
  10.                     list.activityInfo.applicationInfo.packageName,
  11.                     list.activityInfo.name);
  12.             Map<String, Object> temp = new HashMap<String, Object>();

  13.             temp.put("title", list.loadLabel(pm).toString());
  14.             temp.put("icon", list.getIconResource());
  15.             temp.put("intent", intent);

  16.             myData.add(temp);
  17.         }
数据的构造和SimpleAdapter的参数匹配,就是查询设备中的有分享功能的APK,然后读出loadLabel。在数据中加入temp.put("title", list.loadLabel(pm).toString()); 同时加入了temp.put("intent", intent);这个在点击的时候使用intent 弹出分享的界面。
再看一下  int resource 这个参数,代表一个布局文件,这个参数和 data  和 int[] to也要匹配, int[] to就是 resource 中的一个部件。明白了这个几个参数之间的关系,构造相应的数据即可。R.layout.listay的布局:里面只有一个android:id="@android:id/text1",这就是每个列表的布局。

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="wrap_content"
  5.     android:orientation="horizontal" >

  6.     <TextView
  7.         android:id="@android:id/text1"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:gravity="center_vertical"
  11.         android:minHeight="?android:attr/listPreferredItemHeightSmall"
  12.         android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
  13.         android:paddingStart="?android:attr/listPreferredItemPaddingStart"
  14.         android:textAppearance="?android:attr/textAppearanceListItemSmall" />

  15. </LinearLayout>

Screen Layout

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). 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 有一个默认的全屏布局。但是也可以定制,在onCreate 的时候setContentView,但是这个view 必须有一个ID为:@android:id/list  ListView。看一下activity_list_ay.xml:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.          android:orientation="vertical"
  4.          android:layout_width="match_parent"
  5.          android:layout_height="match_parent"
  6.          android:paddingLeft="8dp"
  7.          android:paddingRight="8dp">


  8.      <ListView android:id="@android:id/list"
  9.                android:layout_width="match_parent"
  10.                android:layout_height="match_parent"
  11.                android:background="#00FF00"
  12.                android:layout_weight="1"
  13.                android:drawSelectorOnTop="false"/>

  14.      <TextView android:id="@android:id/empty"
  15.                android:layout_width="match_parent"
  16.                android:layout_height="match_parent"
  17.                android:background="#FF0000"
  18.                android:text="No data"/>
  19.  </LinearLayout>

线性布局中包含了两个view,  < ListView android:id = "@android:id/list" 和 <TextView android:id="@android:id/empty"
ListView ID使用了android 的list。TextView ID为:android:id="@android:id/empty",这个id/empty有一个特殊的地方,当ListView中有数据的时候显示数据,没有数据的时候显示 TextView。看一下效果:








<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(748) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值