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 ...
随机推荐
- aliyun centos14.04 trusty 上安装docker1.12.1
现在apt这边拿到的docker最新版本就是1.12.1 其实本来这次不准备记录了,本以为一帆风顺的安装最后还是遇到了一点坑,aliyun的锅,卡成狗无法下载.青岛机房 1.更新源,然后安装ca-ce ...
- jdk1.8 HashMap的扩容resize()方法详解
/** * Initializes or doubles table size. If null, allocates in * accord with initial capacity target ...
- 洛谷P2397 yyy loves Maths VI (mode)
P2397 yyy loves Maths VI (mode) 题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居 ...
- TortoiseGit连接gitlab,一直要求输入密码
问题背景: 公司使用gitlab作为代码管理平台,安装了TortoiseGit之后,使用正常.但是重启电脑之后,再次使用TortoiseGit操作时总是提醒输入gitlab的账号.如下图: 问题原因: ...
- BZOJ5343[Ctsc2018]混合果汁——主席树+二分答案
题目链接: CTSC2018混合果汁 显然如果美味度高的合法那么美味度低的一定合法,因为美味度低的可选方案包含美味度高的可选方案. 那么我们二分一个美味度作为答案然后考虑如何验证? 选择时显然要贪心的 ...
- poj1850-CODE-组合
求出给定序列的序号.有一个定理需要知道 具体看这篇博客吧http://blog.csdn.net/lyy289065406/article/details/6648492 #include <c ...
- 查看本地Git仓库历史修改内容
查看历史内容 在.git文件 同级目录下,右键 选择 git history 但是红框中的路径无法拷贝.右键红框中的任一文件,有 HighLight this only, Highlight this ...
- linux保存住github的账号和密码
我是用 shell 命令来执行 git 操作的,所以就决定在 --global 配置一下用户名和密码,不用每次手动输入了. 设置步骤 1. 首先需要 ftp 连接到你的 Linux 服务器,在根目录( ...
- luogu1080 国王游戏(贪心+高精度)
貌似这道题是碰巧蒙对了贪心的方式..就是把ai*bi越小的放在越前面 (不过也符合直觉) 然后统计答案需要用高精度,然后就调了一年 #include<cstdio> #include< ...
- 【洛谷P2617】Dynamic Rankings
题目大意:维护带修改区间 K 小值. 题解:学习到了树状数组套权值线段树. 主席树,即:可持久化权值线段树,支持维护静态区间的 K 小值问题,其核心思想是维护 N 棵权值线段树,每个线段树维护的是序列 ...