Android开发之列表控件

Wesley13
• 阅读 561

一、基础知识:

ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。先说说ListView的实现:

1.准备ListView要显示的数据;

2.使用一维或多维动态数组保存数据;

3.构建适配器,简单地来说,适配器就是Item数组,动态数组有多少元素就生成多少个Item;

4.把适配器添加到ListView,并显示出来。

二、代码展示:

1."Activity_10srcyanactivity_10MainActivity.java"

[java]

package yan.activity_10; 

import java.util.ArrayList; 

import java.util.HashMap; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.ListView; 

import android.widget.SimpleAdapter; 

import android.app.ListActivity; 

public class MainActivity extends ListActivity { 

    private final String raw_user_name = "user_name"; 

    private final String raw_user_id = "user_id"; 

    private final String raw_user_ip = "user_ip"; 

    private String user_name_array[]={"zhangsan","lisi","wangwu"}; 

    private String user_ip_array[]={ 

            "192.168.1.115", 

            "192.168.1.116", 

            "192.168.1.117"}; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        // 生成动态数组,并且传入数据  

        ArrayList <HashMap<String,String>> mylistArray = new ArrayList <HashMap<String,String>>(); 

        for(int i=0; i<30; ++i) 

        { 

            HashMap<String,String> map = new HashMap<String,String>(); 

            System.out.println("HAH : " + i%3); 

            map.put(raw_user_name, user_name_array[i%3]); 

            map.put(raw_user_id, i+1+""); 

            map.put(raw_user_ip, user_ip_array[i%3]); 

            mylistArray.add(map); 

        } 

        //生成适配器,数组-->>ListItem  

        SimpleAdapter mSchedule = new SimpleAdapter( 

                this, 

                mylistArray,    //  数据来源  

                R.layout.my_listview,   // ListItem的XML实现  

                new String[] {raw_user_name,raw_user_id,raw_user_ip},   // 动态数组与ListItem对应的子项  

                new int[] {R.id.user_name,R.id.user_id,R.id.user_ip}    // ListItem的XML文件里面的两个TextView ID  

                ); 

        setListAdapter(mSchedule); 

    } 

    @Override 

    protected void onListItemClick(ListView l, View v, int position, long id) { 

        // TODO Auto-generated method stub  

        super.onListItemClick(l, v, position, id); 

        System.out.println("id ---------------- " + id); 

        System.out.println("position ---------------- " + position); 

    } 

package yan.activity_10;

import java.util.ArrayList;

import java.util.HashMap;

import android.os.Bundle;

import android.view.View;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.app.ListActivity;

public class MainActivity extends ListActivity {

 private final String raw_user_name = "user_name";

 private final String raw_user_id = "user_id";

 private final String raw_user_ip = "user_ip";

 private String user_name_array[]={"zhangsan","lisi","wangwu"};

 private String user_ip_array[]={

   "192.168.1.115",

   "192.168.1.116",

   "192.168.1.117"};

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  // 生成动态数组,并且传入数据

  ArrayList <HashMap<String,String>> mylistArray = new ArrayList <HashMap<String,String>>();

  for(int i=0; i<30; ++i)

  {

   HashMap<String,String> map = new HashMap<String,String>();

   System.out.println("HAH : " + i%3);

   map.put(raw_user_name, user_name_array[i%3]);

   map.put(raw_user_id, i+1+"");

   map.put(raw_user_ip, user_ip_array[i%3]);

   mylistArray.add(map);

  }

  //生成适配器,数组-->>ListItem

  SimpleAdapter mSchedule = new SimpleAdapter(

    this,

    mylistArray, // 数据来源

    R.layout.my_listview, // ListItem的XML实现

    new String[] {raw_user_name,raw_user_id,raw_user_ip}, // 动态数组与ListItem对应的子项

    new int[] {R.id.user_name,R.id.user_id,R.id.user_ip} // ListItem的XML文件里面的两个TextView ID

    );

  setListAdapter(mSchedule);

 }

 @Override

 protected void onListItemClick(ListView l, View v, int position, long id) {

  // TODO Auto-generated method stub

  super.onListItemClick(l, v, position, id);

  System.out.println("id ---------------- " + id);

  System.out.println("position ---------------- " + position);

 }

}

2."Activity_10reslayoutmain.xml"

[html]

   

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   

    android:orientation="vertical"   

    android:layout_width="fill_parent"   

    android:layout_height="wrap_content"   

    >   

   <ListView 

       android:id="@+id/android:list" 

       android:layout_width="fill_parent" 

       android:layout_height="wrap_content" 

       android:drawSelectorOnTop="false" 

       android:scrollbars="vertical" 

    /> 

   

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    > 

   <ListView

       android:id="@+id/android:list"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:drawSelectorOnTop="false"

       android:scrollbars="vertical"

    />

  

3."Activity_10reslayoutmy_listview.xml"  my_listview.xml用于设计ListView的Item:

[html]

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="horizontal" 

    android:paddingBottom="3dip" 

    android:paddingTop="1dip" 

    android:paddingLeft="10dip"> 

    <TextView 

        android:id="@+id/user_name" 

        android:layout_width="100dp" 

        android:layout_height="30dp" 

        android:textSize="10pt" 

        android:singleLine="true"/> 

    <TextView 

        android:id="@+id/user_id" 

        android:layout_width="100dp" 

        android:layout_height="30dp" 

        android:textSize="10pt" 

        android:singleLine="true"/> 

    <TextView 

        android:id="@+id/user_ip" 

        android:layout_width="wrap_content" 

        android:layout_height="fill_parent" 

        android:gravity="right" 

        android:textSize="10pt"/> 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal"

    android:paddingBottom="3dip"

    android:paddingTop="1dip"

    android:paddingLeft="10dip">

    <TextView

        android:id="@+id/user_name"

        android:layout_width="100dp"

        android:layout_height="30dp"

        android:textSize="10pt"

        android:singleLine="true"/>

    <TextView

        android:id="@+id/user_id"

        android:layout_width="100dp"

        android:layout_height="30dp"

        android:textSize="10pt"

        android:singleLine="true"/>

 <TextView

        android:id="@+id/user_ip"

        android:layout_width="wrap_content"

        android:layout_height="fill_parent"

        android:gravity="right"

        android:textSize="10pt"/>

三、效果展示:

 Android开发之列表控件

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
达里尔 达里尔
5个月前
给数组添加新数据,判断数据是否重复
多选要进行数组拼接,希望判断往原数组里添的新数据是否重复,封装个简易方法languageconstdataArrayname:'aaa',id:1,name:'bbb',id:2;constnewDataname:'ccc',id:2;//要添加的新数
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这