【安卓9】SimpleCursorAdapter、在列表中展示数据

SimpleCursorAdapter

SimpleCursorAdaper与SimpleAdapter类似,但数据来源是Cursor。

操作步骤:

在列表中展示数据

 1 public MySQLiteOpenHelper(Context context) {
 2      super(context,"person.db",null,1);
 3 }
 4 public void onCreate(SQLiteDatabase db) {
 5     db.execSQL("create table if not exists person("
 6         +"_id integer primary key autoincrement,"
 7         +"name varchar(20)");
 8 }
 9 /**    查询方法
10   * @return Cursor对象
11   */
12  public  Cursor  query(){
13     SQLiteDatabase  db=getWritableDatabase();
14     Cursor cursor=db.query("person", new String[]{"id _id ","name"}, null, null, null, null, null);
15     return cursor;
16  }
17 /**向数据库插入数据*/
18     public void insert(String [] args){
19         SQLiteDatabase db=this.getWritableDatabase();
20         //该对象可操作键-值对数据
21         ContentValues values=new ContentValues();
22         values.put("name",args[0]);//存放数据
23         //表名,强行插入null值得数据列的列名,记录数据
24         db.insert("person",null,values);
25     } 

MySQLiteOpenHelper代码

 1 Activity代码
 2 public class Main extends Activity implements OnClickListener{
 3 private  Button  addBtn;
 4 private  Button  findBtn;
 5 private  EditText nameEt;
 6 private  EditText phoneEt;
 7 private  ListView listView;
 8 private  SimpleCursorAdapter  adapter;
 9 private  MySQLiteOpenHelper  helper;
10     public void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.main);
13         addBtn=(Button)findViewById(R.id.addBtn);
14         findBtn=(Button)findViewById(R.id.findBtn);
15         nameEt=(EditText)findViewById(R.id.nameEt);
16         phoneEt=(EditText)findViewById(R.id.phoneEt);
17         listView=(ListView)findViewById(R.id.lv);
18         addBtn.setOnClickListener(this);
19         findBtn.setOnClickListener(this);
20
21         helper=new MySQLiteOpenHelper(this);
22         Cursor  cursor=helper.query();
23         adapter=new SimpleCursorAdapter(this,R.layout.list_item,
24         cursor,new String[]{"_id","name","phone"},
25         new int[]{R.id.idTv,R.id.nameTv,R.id.phoneTv});
26         listView.setAdapter(adapter);
27     }
28 public void onClick(View v) {
29     MySQLiteOpenHelper  db=new MySQLiteOpenHelper(Main.this);
30     switch(v.getId()){
31     case R.id.addBtn:
32         String  name=nameEt.getText().toString();
33         String  phone=phoneEt.getText().toString();
34         db.insert(new String[]{name,phone});
35         Toast.makeText(this,"插入成功",3000).show();
36     break;
37    case R.id.findBtn:
38            Cursor  cur=helper.query();
39            adapter.changeCursor(cur);//更新游标适配器对象
40            adapter.notifyDataSetChanged();//通知列表控件数据发生改变
41     break;
42 }

Activity 代码

时间: 2025-01-02 13:52:29

【安卓9】SimpleCursorAdapter、在列表中展示数据的相关文章

从列表中提取数据的两种方法

从列表中提取数据除了循环外还有两种方法:过滤,列表解析式: #!/usr/bin/env python #coding:utf-8 #@Author:Andy # 生成一个随机列表:并选出其中的两大于零的数 from random import randint data = [randint(-10, 10) for i in range(1, 10)] print(data) # method 1 print("filter method:", list(filter(lambda

powerdesign 下ER模型中展示数据注释中文列

工作三年了,如果说对于数据库不敏感,我觉得的无论是作为一个前端开发人员还是后端开发人员来说,跟别人说你是个软件开发人员,我觉得都有点惭愧.可能之前一直在关注数据库sql的编写等等,但是现在我觉得有必要升华一下了,下面我就我遇到的问题,向大家提供一个解决方案,可能不是最好的,但是当实现了这个效果后,我真的眼前一亮,在此分享给大家...... 软件环境:powerdesign16.5 + win64(64位系统下,由于jdk的版本,powerdesign软件运行不正常,这个我安装jdk1.6_x86

python列表中的pop函数

再python的列表中,有许多的内置方法,而在这里我主要向大家介绍一下pop函数. pop函数主要是用于删除列表中的数据.而其删除值时会返回删除的值.如果没有参数传入时, 则会默认认为删除列表的最后一项数据. 如: a = ["ss",'nn','cc'] print(a.pop(1))运行结果就是nn如果是print(a.pop())运行结果就是:cc 原文地址:https://www.cnblogs.com/bao9687426/p/9813041.html

Python从文件中读取数据(2)

一.读取文件中各行的内容并存储到一个列表中 继续用resource.txt 举例 resource.txt my name is joker, I am 18 years old, How about you? test.py 1 file_name = 'resource.txt' 2 3 with open(file_name) as file_obj: 4 lines = file_obj.readlines() 5 print(lines) 控制台打印如下: 解释:可以看到在file_o

salesforce lightning零基础学习(七) 列表展示数据时两种自定义编辑页面

上一篇Lightning内容描述的是LDS,通过LDS可以很方便的实例化一个对象的数据信息.当我们通过列表展示数据需要编辑时,我们常使用两种方式去处理编辑页面:Pop Up Window弹出修改详情以及在本页面隐藏详情页面显示编辑页面. 实现这个功能以前主要需要先了解几个标签: lightning:recordForm: 此标签允许用户快速的创建一个form去查看,添加以及修改一条记录.集合了 lightning:recordEditForm 以及 lightning:recordViewFor

【Excle数据透视】如何在数据透视表字段列表中显示更多的字段

创建完数据透视表之后,由于字段太多,在列表中没有完全显示 解决方案 通过"字段节和区域节并排"功能来显示更多字段 修改后结果 字段已经完全显示出来了! "字段节和区域节层叠"字段太多就造成了字段显示不全.

AsyncTask异步;ListView列表展示数据

AsyncTask异步ListView列表展示数据 libs文件夹下加入jar包(导包): gson ======================================= 添加权限: 网络请求权限:INTERNET<uses-permission android:name="android.permission.INTERNET"/>网络判断权限:ACCESS_NETWORK_STATE<uses-permission android:name="

将一个列表的数据复制到另一个列表中

1 ''' 2 提问:将一个列表的数据复制到另一个列表中. 3 请仔细看要求,这里要求的是复制数据到一个新的列表中. 4 Python列表数据复制,Python解题思路分析:可以了解下[ :]的含义 5 ''' 6 l = [1,2,3,4] 7 x = l[:] # x = l.copy() 8 print(x) 原文地址:https://www.cnblogs.com/JerryZao/p/8678880.html

python删除列表中得重复得数据

解决思想:将列表转换为 集合,利用集合删除重复数据得特性删除重复数据,然后将集合转换为列表 #删除列表中得重复元素 def delect_1 (lt): s = set(lt) lt = list(s) print(lt)delect_1([1,2,3,4,1,3,4,5]) 原文地址:https://www.cnblogs.com/chaojiyingxiong/p/9174791.html