zoukankan      html  css  js  c++  java
  • 实例:基于ListActivity实现列表

    如果程序的窗口仅仅需要显示一个列表,则可以直接让Activity继承ListActivity来实现,ListActivity的子类无须调用setContentView()方法来显示某个界面,而是可以直接传入一个I饿内容Adapter,ListActivity的子类就呈现出一个列表。

    例如如下布局文件:

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        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"
      >
    </ListView>

    后台代码如下:

    package org.crazyit.helloworld;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    
    @SuppressWarnings("unused")
    public class ListActivityTest extends ListActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //无须使用布局文件
            String[] arr=new String[]{"孙悟空","猪八戒","唐僧"};
            //创建ArrayAdapter对象
            ArrayAdapter<String> adapter=new ArrayAdapter<String>
            (this,android.R.layout.simple_list_item_multiple_choice,arr);
            //设置该窗口显示列表
            //setContentView(R.layout.list_activity_test);
            setListAdapter(adapter);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.list_activity_test, menu);
            return true;
        }
    
    }

           上面程序的Activity继承了ListActivity,ListActivity无须界面布局文件——相当于它的布局文件中只有一个ListView,因此只要为ListActivity设置Adapter即可。因此只要为ListActivity设置Adapter即可。上面的程序使用Android提供的R.layout.simple_list_multiple_choice布局文件作为列表项组件。

           ListActivity的默认布局是由一个位于屏幕中心的列表组件。实际上,我们完全可以在OnCreate()方法中通过setContentView(int layoutId)方法设置用户的自定义布局。

          需要指出的是,在我们指定的界面布局文件中应该不含一个id为“@+id/android:list”(如果使用代码的形式,则是android.R.id.list)的ListView。

          运行上面的程序出现如下效果:

       

  • 相关阅读:
    FastDFS 集群 安装 配置
    springboot 集成fastDfs
    分布式文件系统FastDFS详解
    上传下载
    Spring Boot:上传文件大小超限制如何捕获 MaxUploadSizeExceededException 异常
    MySQL报错解决方案:2013-Lost connection to MySQL server
    JWT与Session比较和作用
    html跑马灯/走马灯效果
    后端排序,debug模式中map的顺序出错
    js调用后台接口进行下载
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3378034.html
Copyright © 2011-2022 走看看