码迷,mamicode.com
首页 > 移动开发 > 详细

<Android考证之实训项目九> SQLit表中图书信息读取

时间:2019-05-31 23:50:29      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:mic   from   bundle   bst   class   port   set   步骤   编程   

 技术图片 技术图片

技术图片
 1 package com.example.sqlitetest2;
 2 
 3 import android.content.Context;
 4 import android.database.Cursor;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.database.sqlite.SQLiteOpenHelper;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.widget.ListView;
10 import android.widget.SimpleCursorAdapter;
11 
12 public class MainActivity extends AppCompatActivity {
13     ListView list;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         list = findViewById(R.id.list);
19 
20         NoteData noteData = new NoteData(this);
21         SQLiteDatabase db = noteData.getReadableDatabase();
22         Cursor cursor = db.rawQuery("select _id,TITLE, substr(CONTENT,1,10)||‘...‘ as CONTENT, DATETIME from notes",null);
23         String[] from = {"_id","TITLE", "CONTENT", "DATETIME"};
24         int[] to = {R.id._id, R.id.title, R.id.content, R.id.datetime};
25         SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.data,cursor,from,to);
26         list.setAdapter(simpleCursorAdapter);
27     }
28 }
29 class NoteData extends SQLiteOpenHelper{
30 
31     private static final String DATABASE_NAME = "test.com.app3.notedata.db";
32     private static final int DATABASE_VERSION = 1;
33     public NoteData(Context context){
34         super(context,DATABASE_NAME,null,DATABASE_VERSION);
35     }
36 
37     @Override
38     public void onCreate(SQLiteDatabase db) {
39         db.execSQL("create table notes(_id integer primary key autoincrement," +
40                 "TITLE text not null," +
41                 "CONTENT text not null," +
42                 "DATETIME text not null);");
43         db.execSQL("insert into notes(TITLE,CONTENT,DATETIME) values(‘阅读小记‘,‘久闻《编程珠玑》一书的大名," +
44                 "一直没有找到合适的机会深入学习阅读,最近终于得以入手,便决心投入细细的研究,提升一下自己的变成思想技术。‘,‘2017-03-28‘)");
45         db.execSQL("insert into notes(TITLE,CONTENT,DATETIME) values(‘程序调试‘,‘程序调试时将编制的程序投入" +
46                 "实际运行前,用手工或编译程序等方法进行测试,修正语法错误和逻辑错误的过程。这是保证计算机信息系统" +
47                 "正确性的必不可少的步骤。‘,‘2017-03-28‘)");
48         db.execSQL("insert into notes(TITLE,CONTENT,DATETIME) values(‘英语学习‘,‘既然想学习英语,那么就对自己狠一点。找一些" +
49                 "高难度的或者全英语的视频电影来看,还有的时可以看英语新闻。‘,‘2017-03-31‘)");
50     }
51 
52     @Override
53     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
54         db.execSQL("drop table if exists notes");
55         onCreate(db);
56     }
57 }
MainActivity.java
技术图片
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <ListView
10         android:id="@+id/list"
11         android:layout_width="match_parent"
12         android:layout_height="match_parent" />
13 </android.support.constraint.ConstraintLayout>
activity_main.xml
技术图片
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5 
 6     <TextView
 7         android:id="@+id/_id"
 8         android:layout_width="5dp"
 9         android:layout_height="match_parent"
10         android:layout_weight="1"
11         android:gravity="center"
12         android:textSize="18sp"
13         android:text="TextView" />
14 
15     <LinearLayout
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_weight="1"
19         android:orientation="vertical">
20 
21         <TextView
22             android:id="@+id/title"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:textSize="18sp"
26             android:text="TextView" />
27 
28         <TextView
29             android:id="@+id/content"
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:textSize="18sp"
33             android:text="TextView" />
34     </LinearLayout>
35 
36     <TextView
37         android:id="@+id/datetime"
38         android:layout_width="wrap_content"
39         android:layout_height="wrap_content"
40         android:textSize="18sp"
41         android:layout_weight="1"
42         android:text="TextView" />
43 
44 </LinearLayout>
data.xml

 

<Android考证之实训项目九> SQLit表中图书信息读取

标签:mic   from   bundle   bst   class   port   set   步骤   编程   

原文地址:https://www.cnblogs.com/jdxb/p/10957746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!