项目步骤

  1. 声明listView控件并获取显示的视图
  2. 获取显示的数据
  3. 设置显示的adapter
  4. 注冊点击事件

详细案例

实现效果:

查找的方法

public List<Map<String, Object>> getData() {
SQLiteDatabase db = dh.getWritableDatabase();
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Cursor c = db
.rawQuery(
"select userid ,username,userage,usersalary,userphone from users",
null); while (c.moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", c.getInt(c.getColumnIndex("userid")));
map.put("name", c.getString(c.getColumnIndex("username")));
map.put("age", c.getInt(c.getColumnIndex("userage")));
map.put("phone", c.getString(c.getColumnIndex("userphone")));
data.add(map);
}
db.close();
return data;
}

布局文件

同BaseAdapter的布局文件activity_main.xml

ArrayAdapter实现类(主程序)

package com.example.android_sqlite;

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.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; import com.example.android_sqlite.dao.impl.UserDaoImpls;
import com.example.android_sqlite.database.DatabaseHelper; /**
* SimpleAdapter
*
* @author zhaoyazhi
*
*/
public class MainActivity extends Activity implements OnItemClickListener {
private ListView lv_users;// 视图层 // model层 相关的对象的声明
private DatabaseHelper dh;
private UserDaoImpls userDao; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 模型层
dh = new DatabaseHelper(this);// 创建数据库
userDao = new UserDaoImpls(dh);// 获取显示数据 model
List<Map<String, Object>> data = userDao.getData();// 查询获取数据 // 控制层 依赖于模型层
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.item_phone, new String[] { "name", "phone", "id",
"age" }, new int[] { R.id.tv_name, R.id.tv_phone,
R.id.tv_id, R.id.tv_age }); // 视图层 依赖于控制层
lv_users = (ListView) findViewById(R.id.lv_users);
lv_users.setAdapter(adapter); // 注冊点击事件
lv_users.setOnItemClickListener(this); } @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) { // 获取点击我的数据
Toast.makeText(this, parent.getItemAtPosition(position) + "", 0).show(); } }

赵雅智_ListView_SimpleAdapter的更多相关文章

  1. 赵雅智_ContentProvider

    ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...

  2. 赵雅智_Fragment生命周期

    官网帮助文档链接:  http://developer.android.com/guide/components/fragments.html 主要看两张图.和跑代码 一,Fragment的生命周 w ...

  3. 赵雅智:js知识点汇总

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  4. 赵雅智_ListView_BaseAdapter

    Android界面中有时候须要显示略微复杂的界面时,就须要我们自己定义一个adapter,而此adapter就要继承BaseAdapter,又一次当中的方法. Android中Adapter类事实上就 ...

  5. 赵雅智_BroadcastReceiver电话监听

    AndroidManifest.xml 注冊广播接收者 加入权限 <?xml version="1.0" encoding="utf-8"?> &l ...

  6. 赵雅智_BroadcastReceiver

    BroadcastReceiver  用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...

  7. 赵雅智:service_startService生命周期

    案例演示 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  8. 赵雅智_BroadcastReceiver短信监听

    AndroidManifest.xml 注冊广播接收者 加入权限 <?xml version="1.0" encoding="utf-8"?> &l ...

  9. 赵雅智_Android编码规范

    凝视 导入mycodetemplates.xml统一凝视样式 须要加凝视的地方 类凝视(必加) 方法凝视(必加) 块凝视主要是数据结构和算法的描写叙述(必加) 类成员变量和常量凝视(选择性加入) 单行 ...

随机推荐

  1. jdbc数据库中的增删改

    HttpSession session=request.getSession(); session.setAttribute("currentUser",u.username);/ ...

  2. TPshop规格组合错误

    TPshop规格组合错误 修改: admin/logic/goodslogic.class.php 中 方法:getSpecInput() 中 asort($spec_arr_sort) 去掉

  3. Win10重置 系统诸多设置或者菜单点击无效或者异常信息回复办法

    cmd: 输入下列脚本重新注册DLL文件,待执行完毕后重启电脑 for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1

  4. Mvc程序字体加载失败问题

    在我们开发的asp.net-mvc项目中,有时会出现字体加载失败的现象,但是一检查字体文件目录,发现文件目录都是存在的且有效的,这是为何呢?原来需要再web.config文件中添价少许配置代码就搞定. ...

  5. B - Link/Cut Tree

    Problem description Programmer Rostislav got seriously interested in the Link/Cut Tree data structur ...

  6. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  7. 7.union

    联合结果集union 简单的结果集联合: select number,name,age from emp union select cardnumber,name,age from emp2 基本的原 ...

  8. MySql-Connector for NET 连接驱动选择

    尝试在Visual Studio2010, 2012环境下链接Mysql, 为啥不直接在App.config里面写字符串, 当然是可以,但是当你想用EF 的时候,必须要有个数据源, 首先在[服务资源管 ...

  9. poj 1564 Sum It Up 搜索

    题意: 给出一个数T,再给出n个数.若n个数中有几个数(可以是一个)的和是T,就输出相加的式子.不过不能输出相同的式子. 分析: 运用的是回溯法.比较特殊的一点就是不能输出相同的式子.这个可以通过ma ...

  10. Excel常用的小技巧

    1.Excel如何实现单元格内轻松换行:按住ALT+enter就可以了. 2.Excel固定表头:在“视图”>冻结窗口>冻结首行. 3.防止电脑突然断电,导致正在编辑的Excel数据丢失, ...