我的Android进阶之旅——>Android ListView 应用解析(使用ArrayAdapter,SimpleAdapter和SimpleCursorAdapter适配器)…

我的Android进阶之旅——>Android ListView 应用解析(使用ArrayAdapter,SimpleAdapter和SimpleCursorAdapter适配器)…

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。抽空把对ListView的使用做了整理,并写了个小例子,如下图。

我的Android进阶之旅------>Android ListView 应用解析(使用ArrayAdapter,SimpleAdapter和SimpleCursorAdapter适配器)...

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

 

现在项目的结构如下图所示:

我的Android进阶之旅------>Android ListView 应用解析(使用ArrayAdapter,SimpleAdapter和SimpleCursorAdapter适配器)...

 

main.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">

	<LinearLayout android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="wrap_content">
		<TextView android:text="@string/name" android:layout_width="120dp"
			android:layout_height="wrap_content" />
		<TextView android:text="@string/phone" android:layout_width="fill_parent"
			android:layout_height="wrap_content" />
	</LinearLayout>

	<ListView android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:id="@+id/listView" />
</LinearLayout>

 

item.xml如下图所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/name" android:layout_width="120dp"
		android:layout_height="wrap_content"/>
	<TextView android:id="@+id/phone" android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
</LinearLayout>

 

 string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">数据库应用</string>
    <string name="name">姓名</string>
    <string name="phone">电话</string>
</resources>

 

 

cn.roco.db.domain.Person代码如下:

package cn.roco.db.domain;

public class Person {
	public Person(Integer id, String name, String phone) {
		this.id = id;
		this.name = name;
		this.phone = phone;
	}
	public Person(String name, String phone) {
		this.name = name;
		this.phone = phone;
	}
	public Person() {
	}

	private Integer id;
	private String name;
	private String phone;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Person[id="+id+",name="+name+",phone="+phone+"]";
	}
}

package cn.roco.db.service;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {

	public DBOpenHelper(Context context) {
		super(context,"roco.db", null, 2);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE person(personid integer primary key autoincrement,name varchar(20),phone varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL");
	}

}

 

 

package cn.roco.db.serice;

import java.util.List;

import cn.roco.db.domain.Person;

public interface IPersonService {
	public void save(Person person);
	public void delete(Integer id);
	public void update(Person person);
	public Person find(Integer id);
	public List<Person> getScrollData(int offset,int maxResult);
	public long getCount();
}

 

 

package cn.roco.db.serice.imp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.roco.db.domain.Person;
import cn.roco.db.serice.IPersonService;
import cn.roco.db.service.DBOpenHelper;

public class PersonServiceImp implements IPersonService {
	private DBOpenHelper dbOpenHelper;

	public PersonServiceImp(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}
	/**
	 * 添加记录
	 * @param person
	 */
	@Override
	public void save(Person person) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL("insert into person(name,phone) values(?,?)", new Object[] {
				person.getName(), person.getPhone() });
	}
	/**
	 * 添加记录
	 * @param id 
	 */
	@Override
	public void delete(Integer id) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL("delete from person where personid=?", new Object[] { id });
	}
	/**
	 * 更新记录
	 * @param person
	 */
	@Override
	public void update(Person person) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execSQL(
				"update person set name=?,phone=? where personid=?",
				new Object[] { person.getName(), person.getPhone(),
						person.getId() });
	}
	/**
	 * 查询记录
	 * @param id
	 */
	@Override
	public Person find(Integer id) {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select personid,name,phone from person where personid=?",
				new String[] { id.toString() });
		if (cursor.moveToFirst()) {
			int personid = cursor.getInt(cursor.getColumnIndex("personid"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			return new Person(personid, name, phone);
		}
		cursor.close();
		return null;
	}
	/**
	 * 分页获取几率
	 * @param offset 跳过前面多少条记录
	 * @param maxResult 每页获取多少条记录
	 * @return 
	 */
	@Override
	public List<Person> getScrollData(int offset, int maxResult) {
		List<Person> persons = new ArrayList<Person>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select * from person order by personid asc limit ?,?",
						new String[] { String.valueOf(offset),
								String.valueOf(maxResult) });
		while (cursor.moveToNext()) {
			int personid = cursor.getInt(cursor.getColumnIndex("personid"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			persons.add(new Person(personid, name, phone));
		}
		cursor.close();
		return persons;
	}
	
	public Cursor getCursorScrollData(int offset, int maxResult) {
		List<Person> persons = new ArrayList<Person>();
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select personid as _id, name, phone from person order by personid asc limit ?,?",
						new String[] { String.valueOf(offset),
								String.valueOf(maxResult) });
		return cursor;
	}
	
	/**
	 * 获取记录总数
	 * @return
	 */
	@Override
	public long getCount() {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from person ", null);
		cursor.moveToFirst();
		long result = cursor.getLong(0);
		cursor.close();
		return result;
	}

}

MainActivity

 

package cn.roco.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.roco.db.adapter.PersonAdapter;
import cn.roco.db.domain.Person;
import cn.roco.db.serice.imp.PersonServiceImp;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ListView listView;
	private PersonServiceImp personService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		personService = new PersonServiceImp(this);

		listView = (ListView) this.findViewById(R.id.listView);
		listView.setOnItemClickListener(new ItemClickListener());
		
		showByCursor();
//		showByList();
//		showByPersonAdapter();
	}
	/**
	 *配置一个响应事件类
	 */
	private final class ItemClickListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ListView lView=(ListView) parent;
			
			/**
			 * 对应自定义适配器
			 */
//			Person person= (Person) lView.getItemAtPosition(position);
//			Toast.makeText(getApplicationContext(), "id="+person.getId(), 1).show();
			/**
			 * 对应游标适配器
			 */
			Cursor cursor=(Cursor) lView.getItemAtPosition(position);
			int personid=cursor.getInt(cursor.getColumnIndex("_id"));
			Toast.makeText(getApplicationContext(), "id="+personid, 1).show();
		}
	}
	
	
	//使用自定义的适配器PersonAdapter
	private void showByPersonAdapter() {
		List<Person> persons=personService.getScrollData(0, 30);
		PersonAdapter adapter=new PersonAdapter(this, persons, R.layout.item);
		listView.setAdapter(adapter);
	}
	//使用游标适配器SimpleCursorAdapter
	private void showByCursor() {
		Cursor cursor = personService.getCursorScrollData(0, 30);
		
		/**
		 * 注意该适配器中的cursor必须要包含 _id 字段
		 * 解决方法:select personid as _id .....
		 */
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.item, cursor, new String[] { "name", "phone" },
				new int[] { R.id.name, R.id.phone });
		listView.setAdapter(adapter);
	}
	//使用简单适配器SimpleAdapter
	private void showByList() {
		List<Person> persons = personService.getScrollData(0, 30);

		/**
		 * 将取出来的数据封装到 List<HashMap<String, Object>> 中
		 */
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (Person person : persons) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("name", person.getName());
			item.put("phone", person.getPhone());
			item.put("id", person.getId());
			data.add(item);
		}
		/**
		 * 设置好适配器对应的关系
		 */
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
				new String[] { "name", "phone" }, new int[] { R.id.name,
						R.id.phone });
		listView.setAdapter(adapter);
	}
}

 自定义的Adapter

package cn.roco.db.adapter;

import java.util.List;

import cn.roco.db.R;
import cn.roco.db.domain.Person;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PersonAdapter extends BaseAdapter {
	private List<Person> persons; //绑定数据
	private int resouce ; //绑定的条目节目
	private LayoutInflater inflater;
	
	public PersonAdapter(Context context, List<Person> persons,int resouce) {
		this.persons=persons;
		this.resouce=resouce;
		inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	//得到总的数量
	public int getCount() {
		return persons.size();   //数据总数
	}

	@Override
	//根据ListView位置返回View
	public Object getItem(int position) {
		return persons.get(position);
	}

	@Override
	 //根据ListView位置得到List中的ID
	public long getItemId(int position) {
		return position;
	}

	@Override
	 //根据位置得到View对象
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView nameView=null;
		TextView phoneView=null;
		if (convertView==null) {
			convertView=inflater.inflate(resouce, null);//生成条目节目对象
			 //得到条目中的子组件
			nameView=(TextView) convertView.findViewById(R.id.name);
			phoneView= (TextView) convertView.findViewById(R.id.phone);
			
			ViewCache cache=new ViewCache();
			cache.nameView=nameView;
			cache.phoneView=phoneView;
			convertView.setTag(cache);
		}else{
			ViewCache cache=(ViewCache) convertView.getTag();
			nameView=cache.nameView;
			phoneView=cache.phoneView;
		}
		//从list对象中为子组件赋值  实现数据绑定
		Person person= persons.get(position);
		nameView.setText(person.getName());
		phoneView.setText(person.getPhone());
		
		return convertView;
	}
	/**
	 * 定义的缓存类
	 */
	private final class ViewCache{
		public TextView nameView;
		public TextView phoneView;
	}

}

 

 

 

 

==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================

 

转载于:https://www.cnblogs.com/ouyangpeng/archive/2013/03/30/8538428.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110174.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 通常每个套接字地址只允许使用一次_max无法写入配置文件

    通常每个套接字地址只允许使用一次_max无法写入配置文件写入配置文件 CString IP, Port; UpdateData(TRUE); // 将应用程序控件上面的IP和端口 更新至对应的成员变量 IP = m_IpAddress.GetString(); // 读取成员变量控件上面的IP地址将赋给strIP变量 Port.Format(L”%d”, m_iPort); // 读取成员变量控件上面的端口将赋给m_iPort变量 Wri…

    2022年8月18日
    10
  • zabbix监控jmx

    zabbix监控jmx背景:目前公司用的主要语言就是java,然后在运维过程中会遇到频繁的内存溢出的情况,之前使用过elk日志分析系统可以实时的判断出内存溢出的情况,但是无法查看内存的使用情况,只能通过dump文件查看内存溢出的时候dump下来的文件去分析。这样也无法准确的判断出问题。zabbix可以监控java,并且将内存的使用情况实时的展现出来,这是一个不错的选择。JMX的全称是JavaManagement…

    2022年5月23日
    33
  • android蓝牙通讯开发(详细)「建议收藏」

    android蓝牙通讯开发(详细)「建议收藏」新建一个工程之后,我们可以先看到界面左边的项目栏,我们可以看到,除了app目录以外,大多数的文件和目录都是自动生成的,我们也不需要对他们进行修改,而app目录之下的文件才是我们工作的重点。下面,我先对

    2022年7月1日
    69
  • mysql基础知识笔记

    mysql基础知识笔记

    2022年2月21日
    52
  • 一个不简单的Procedure body例子

    一个不简单的Procedure body例子1createorreplacepackagebodyCountBankData_20150617is2typecursorCommonisrefcursor;–游标类型3strSQLvarchar2(7000);–sql语句变量4strTemp…

    2026年1月24日
    3
  • pip怎么卸载安装包_pip删除安装包

    pip怎么卸载安装包_pip删除安装包1、pip下载安装1.1pip下载进入https://pypi.python.org/pypi/pip,下载.tar.gz压缩包1.2Linux安装pip#tar-xzvfpip-1.5.4.tar.gz解压#cdpip-1.5.4进入解压文件#pythonsetup.pyinstall安装1.3升级pippython-mpipinstall–upgradepip2.pi…

    2022年10月9日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号