展会信息港展会大全

Android ListView详解
来源:互联网   发布日期:2015-11-27 14:02:49   浏览:1859次  

导读:在android应用中,数据以列表的形式显示通常是通过ListView来实现,下面是一个ListView入门的小例子1.首先是主布局文件main.xmlXml代码<?xml...

在android应用中,数据以列表的形式显示通常是通过ListView来实现,下面是一个ListView入门的小例子

1.首先是主布局文件main.xml

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"

>

<RelativeLayout

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

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:layout_width="60px"

android:layout_height="wrap_content"

android:id="@+id/idTitle"

android:text="编号"

/>

<TextView

android:layout_width="150px"

android:layout_height="wrap_content"

android:layout_alignTop="@id/idTitle"

android:layout_toRightOf="@id/idTitle"

android:id="@+id/name"

android:text="姓名"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignTop="@id/name"

android:layout_toRightOf="@id/name"

android:text="年龄"

/>

</RelativeLayout>

<ListView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/listView"

/>

</LinearLayout>

2.然后是列表所要用到的布局文件,在layout目录下新建personitem.xml文件,代码如下

Xml代码

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

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

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:layout_width="60px"

android:layout_height="wrap_content"

android:id="@+id/personid"

/>

<TextView

android:layout_width="150px"

android:layout_height="wrap_content"

android:layout_alignTop="@id/personid"

android:layout_toRightOf="@id/personid"

android:id="@+id/personname"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignTop="@id/personname"

android:layout_toRightOf="@id/personname"

android:id="@+id/personage"

/>

</RelativeLayout>

用到的实体bean为Person,代码如下

Java代码

package com.lamp.domain;

public class Person {

private Integer personid = null;

private String name = null;

private Integer age = null;

public Person() {

}

public Person(String name,Integer age){

this.name = name;

this.age = age;

}

public Integer getPersonid() {

return personid;

}

public void setPersonid(Integer personid) {

this.personid = personid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

@Override

public String toString() {

return "name: " + this.name + ", age:" + this.age;

}

}

3.接着是Activity代码

Java代码

package com.lamp.db;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.database.Cursor;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.SimpleCursorAdapter;

import com.lamp.domain.Person;

import com.lamp.service.PersonService;

public class DBActivity extends Activity {

private ListView listView = null;

private PersonService personService = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

personService = new PersonService(this);

listView = (ListView) this.findViewById(R.id.listView);

List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();

List<Person> personList = personService.getScrollData(0, 10);

HashMap<String,String> map = null;

for(Person person : personList){

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

map.put("personid", String.valueOf(person.getPersonid()));

map.put("name", person.getName());

map.put("age", String.valueOf(person.getAge()));

data.add(map);

}

SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,

R.layout.personitem,

new String[] { "personid", "name", "age" }, new int[]{R.id.personid,R.id.personname,R.id.personage});

listView.setAdapter(simpleAdapter);

/*

* 这是通过游标在ListView控件上显示数据,需要注意的是主键字段名id应改为_id,否则会报错

Cursor cursor = personService.getRawScrollData(0, 10);

SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.personitem,

cursor,

new String[] { "_id", "name", "age" }, new int[]{R.id.personid,R.id.personname,R.id.personage});

listView.setAdapter(simpleCursorAdapter);

*/

//为item选项设置监听

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> parent, View view, int position,

long id) {

ListView listView = (ListView)parent;

Map<String,String> map = (HashMap<String, String>)listView.getItemAtPosition(position);

Log.i("DBActivity", map.get("age"));

}

});

}

}

4.业务层代码如下

Java代码

package com.lamp.service;

import java.util.ArrayList;

import java.util.List;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import com.lamp.domain.Person;

public class PersonService {

private DataBaseOpenHelper dbHelper = null;

private SQLiteDatabase sqLiteDatabase = null;

public PersonService(Context context) {

dbHelper = new DataBaseOpenHelper(context);

sqLiteDatabase = dbHelper.getWritableDatabase();

}

public void save(Person person){

String sql = "insert into person(name,age) values (?,?)";

sqLiteDatabase.execSQL(sql, new Object[]{person.getName(),person.getAge()});

}

//指定删除

public void delete(Integer personid){

String sql = "delete from person where personid=?";

sqLiteDatabase.execSQL(sql,new Object[]{personid});

}

//批量删除

public void deletePersons(Integer... ids){

StringBuilder sb = new StringBuilder();

if(ids.length > 0){

for (int i = 0; i < ids.length; i++) {

sb.append('?').append(',');

}

sb.deleteCharAt(sb.length()-1);

String sql = "delete from person where personid in ("+ sb +")";

sqLiteDatabase.execSQL(sql,(Object[])ids);

}

}

//更新数据

public void update(Person person){

String sql = "update person set name=?,age=? where personid=?";

sqLiteDatabase.execSQL(sql, new Object[]{person.getName(),person.getAge(),person.getPersonid()});

}

//得到全部的结果集

public List<Person> getAllPersons(){

String sql = "select name,age from person";

Cursor cursor = sqLiteDatabase.rawQuery(sql, null);

Person person = null;

List<Person> personList = new ArrayList<Person>();

while(cursor.moveToNext()){

String name = cursor.getString(0);

int age = cursor.getInt(1);

person = new Person(name,age);

personList.add(person);

}

return personList;

}

//分页取数据

public List<Person> getScrollData(Integer start, Integer size){

String sql = "select personid, name,age from person limit ?,?";

Cursor cursor = sqLiteDatabase.rawQuery(sql, new String[]{String.valueOf(start),String.valueOf(size)});

List<Person> personList = new ArrayList<Person>();

Person person = null;

while(cursor.moveToNext()){

int personid = cursor.getInt(0);

String name = cursor.getString(1);

int age = cursor.getInt(2);

person = new Person(name,age);

person.setPersonid(personid);

personList.add(person);

}

return personList;

}

//返回游标对象,当要在ListView中显示数据时,主键的字段名需设定为_id,否则返回的Cursor对象会出错

public Cursor getRawScrollData(Integer start, Integer size){

String sql = "select personid as _id, name,age from person limit ?,?";

return sqLiteDatabase.rawQuery(sql, new String[]{String.valueOf(start),String.valueOf(size)});

}

//返回表中总记录条数

public int getCount(){

String sql = "select count(*) from person";

Cursor cursor = sqLiteDatabase.rawQuery(sql, null);

if(cursor.moveToNext()){

return cursor.getInt(0);

}

return 0;

}

}

5.由于用到了android的SQLite数据库,对数据库操作的工具类代码如下Java代码

package com.lamp.service;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper extends SQLiteOpenHelper {

private static final String DBNAME = "android";

private static final int VERSION = 1;

public DataBaseOpenHelper(Context context) {

super(context, DBNAME, null, VERSION);

}

@Override

public void onCreate(SQLiteDatabase sqLiteDatabase) {

String sql = "create table person (personid integer primary key autoincrement,name varchar(20),age integer)";

sqLiteDatabase.execSQL(sql);

}

@Override

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

String sql = "drop table if exists person";

sqLiteDatabase.execSQL(sql);

onCreate(sqLiteDatabase);

}

}

首先通过单元测试往数据库中添加若干条记录,然后运行项目看到记录以列表的形式显示

作者“坐如松,动如风”

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港