android ListView使用
1.DbOpenHelper
package dbOpenHelper; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; /**
* Created by Administrator on 2014/8/25.
*/
public class DbOpenHelper extends SQLiteOpenHelper { public DbOpenHelper(Context context)
{
//Context 上下文
//name 数据库名
//CursorFactory 游标工厂模式 当为null时 使用默认值
//version 数据库版本 版本号从1开始
super(context, "person.db", null, 1);
} @Override
public void onCreate(SQLiteDatabase db) {
//create table person(id integer primary key autoincrement,name varchar(20),age int,phone varchar(11))
String sql = "create table person(id integer primary key autoincrement,name varchar(20),int age,phone varchar(11))";
db.execSQL(sql);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
2.entity
package domain; /**
* Created by Administrator on 2014/8/25.
*/
public class Person {
private int id;
private String name;
private int age;
private String phone; public Person(){} public Person(int id, String name, int age, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
} public Person(int id, String name, String phone) {
this.id = id;
this.name = name;
this.phone = phone;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} @Override
public String toString() {
return "个人信息{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
'}';
}
}
3.DAO
package dao; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dbOpenHelper.DbOpenHelper;
import domain.Person; import java.util.ArrayList;
import java.util.List; /**
* Created by Administrator on 2014/8/25.
*/
public class PersonDao {
private DbOpenHelper helper; public PersonDao(Context context)
{
helper = new DbOpenHelper(context);
} public void add(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "insert into person(name,phone) values(?,?)";
db.execSQL(sql,new Object[]{p.getName(),p.getPhone()});
db.close();
} public void update(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "update person set phone=? where name=?";
db.execSQL(sql,new Object[]{p.getPhone(),p.getName()});
db.close();
} public void delete(String name)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "delete from person where name=?";
db.execSQL(sql,new Object[]{name});
db.close();
} public Person findByName(String name)
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "select * from person where name=?";
Cursor cursor = db.rawQuery(sql,new String[]{name});
Person p = null;
while(cursor.moveToFirst())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
p = new Person(id,name,phone);
}
db.close();
return p;
} public List<Person> findAll()
{
SQLiteDatabase db = helper.getWritableDatabase();
String sql = "select * from person";
Cursor cursor = db.rawQuery(sql,null);
List<Person> list = new ArrayList<Person>();
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Person p = new Person(id,name,phone);
list.add(p);
}
db.close();
return list;
} }
4.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:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/id"
android:id="@+id/tv_id"
/>
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/name"
android:id="@+id/tv_name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/phone"
android:id="@+id/tv_phone"
/> </LinearLayout>
5.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:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/id"
/>
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/phone"
/>
</LinearLayout> <ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list"
/> </LinearLayout>
6.mainActivity.java
package com.example.dbTest; import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import dao.PersonDao;
import domain.Person;
import service.PersonAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by Administrator on 2014/8/25.
*/
public class PersonListActivity extends Activity { private PersonDao dao;
private ListView lv;
private List<Person> persons; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview); lv = (ListView)this.findViewById(R.id.list);
dao = new PersonDao(this);
persons = dao.findAll();
//show();
//对单个对象绑定点击事件
lv.setOnItemClickListener(new MyItemClickListener());
show2(); } public class MyItemClickListener implements AdapterView.OnItemClickListener
{ @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView lView = (ListView)parent;
Person p = (Person)lView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),p.getName(),0).show();
}
} private void show2() {
PersonAdapter adapter = new PersonAdapter(this,persons,R.layout.item);
lv.setAdapter(adapter);
} private void show() {
List<Person> persons = dao.findAll();
List<HashMap<String,Object>>data = new ArrayList<HashMap<String,Object>>();
for (Person p:persons)
{
HashMap<String,Object> item = new HashMap<String,Object>();
item.put("id",p.getId());
item.put("name",p.getName());
item.put("phone",p.getPhone());
data.add(item);
}
SimpleAdapter sa = new SimpleAdapter(this,data,R.layout.item,new String[]{"id","name","phone"},new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_phone});
lv.setAdapter(sa);
}
}
6.继承BaseAdapter适配器
package service; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.dbTest.R;
import domain.Person; import java.util.List; /**
* Created by Administrator on 2014/8/26.
*/
public class PersonAdapter extends BaseAdapter {
private List<Person> persons;
private int resource;
private LayoutInflater inflater; public PersonAdapter(Context context,List<Person> persons, int resource) {
this.persons = persons;
this.resource = resource;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
return persons.size();
} @Override
public Object getItem(int position) {
return persons.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView = inflater.inflate(resource,null);
}
TextView idView = (TextView)convertView.findViewById(R.id.tv_id);
TextView nameView = (TextView)convertView.findViewById(R.id.tv_name);
TextView phoneView = (TextView)convertView.findViewById(R.id.tv_phone); Person p = persons.get(position); idView.setText(p.getId()+"");
nameView.setText(p.getName());
phoneView.setText(p.getPhone()); return convertView;
}
}
android ListView使用的更多相关文章
- android ListView 九大重要属性详细分析、
android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...
- Android ListView onItemClick Not Work
Android ListView onItemClick Not Work ListView item中有Button和RadioButton的时候,它的Item点击事件不起作用,需要设置item的属 ...
- 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...
- Android ListView 常用技巧
Android ListView 常用技巧 Android TextView 常用技巧 1.使用ViewHolder提高效率 ViewHolder模式充分利用了ListView的视图缓存机制,避免了每 ...
- Android listview addHeaderView 和 addFooterView 详解
addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 需要注意的是添加布局的时候应该添加从父容器开始添加 ...
- Android ListView滑动过程中图片显示重复错乱闪烁问题解决
最新内容建议直接访问原文:Android ListView滑动过程中图片显示重复错乱闪烁问题解决 主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及L ...
- Android --ListView分页
参考博客:Android ListView分页加载(服务端+android端)Demo 监听OnScrollListener事件 class OnListScrollListener implemen ...
- Android ListView ListActivity PreferenceActivity背景变黑的问题ZT
Android ListView ListActivity PreferenceActivity背景变黑的问题 ListView在滚动时背景会变暗甚至变黑,这个要从Listview的效果说起,默认的L ...
- android listview去掉分割线
1:android listview去掉分割线 1>设置android:divider="@null" 2>android:divider="#0000000 ...
- 【转】android ListView 几个重要属性
android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...
随机推荐
- KNN python实践
本文实现了一个KNN算法,准备用作词频统计改进版本之中,这篇博文是从我另一个刚开的博客中copy过来的. KNN算法是一个简单的分类算法,它的动机特别简单:与一个样本点距离近的其他样本点绝大部分属于什 ...
- python 深入浅出装饰器(decorator)--举的例子关于星级争霸2(starcraft2)
其实早就想写一篇深入浅出装饰器的文章,苦于一直没有找到很好的例子描述,自己除了在写api参数检测和日志打印的时候用到以外,其他地方也没有什么重度使用所以一直没有写. 我不会讲解装饰器的理论,还有各种基 ...
- GUI and Usability Test Scenarios
1 all fields on page (e.g. text box ,radio options, dropdown lists) should be aligned properly2 Nume ...
- TP5报错总结
LNMP一键安装包上部署TP5项目500错误或者空白解决 [问题原因] TP5的入口文件在public下,当他调用类文件时,跨目录所以造成500错误,或者一片空白的问题 [解决方法] 1.public ...
- 自学Python3.2-函数分类(内置函数)
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- android 让真机显示 DeBug Log调试信息
真机默认是不开启Log 开关的,这么来说我们如果使用真机来搞程序测试的话,需要做以下几个步骤: 下面以华为手机为例开启手机的log功能: 1.在拨号界面输入:*#*#2846579#*#* 进入测 ...
- Android 第三课 构建简单的用户界面
构建简单的用户界面 上一课下一课 该课程教你 创建线性布局 添加文本框 添加字符串资源 添加按钮 使输入框宽度充满整个屏幕 你也应该阅读 布局 Android的图形用户界面通过 View 和 View ...
- XML解析(DOM、ElementTree)及转换为JSON
xml.dom篇 DOM是Document Object Model的简称,XML 文档的高级树型表示.该模型并非只针对 Python,而是一种普通XML 模型.Python 的 DOM 包是基于 S ...
- suoi38 卖XY序列 (贪心+前缀和)
因为只能带一个,买卖价格又一样,所以只要右边的比左边的大,就从这买下来然后带到下一个卖掉就行了(我想到别处再卖的话大不了再重新买回来嘛) 所以给max(w[i]-w[i-1],0)维护一个前缀和就行了 ...
- JS循环中使用bind函数的参数传递问题
JS循环中使用bind函数的参数传递问题,问题代码如下: for (var sc in result) { var tempp = '<div class="sidebar_todo_ ...