MainActivity.java :代码如下
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText nameET;
private EditText countET;
private MyopenHelper MyopenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameET=findViewById(R.id.name); //实例化对象
countET=findViewById(R.id.count);
MyopenHelper=new MyopenHelper(this,"BookStore.db",null,1); //表名,版本号
}
public void insert(View view) {
String sql="insert into Books(name,count)values (?,?) ";
String name=nameET.getText().toString().trim(); //添加文本框,去点空格
String ccount=countET.getText().toString().trim();
int count=Integer.parseInt(ccount);
MyopenHelper.getWritableDatabase().execSQL(sql,new Object[]{name,count});
Intent intent=new Intent(this,BookShowActivity.class); //intent 连接,跳转到下一个页面
startActivity(intent);
}
public void test(View view) {
String sql="select id,name,count from books";
Cursor c=MyopenHelper.getReadableDatabase().rawQuery(sql,null);
while (c.moveToNext()){
int id=c.getInt(0);
String name=c.getString(1);
int count=c.getInt(2);
Log.i("data","test:"+id+"...."+name +"...."+count);
}
}
}
activity_main.XML代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lnjr.sqlit.MainActivity"
android:orientation="vertical">
// hint 提示框 ,添加 文本输入框
<EditText
android:id="@+id/name"
android:hint="please input name"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/count"
android:hint="please input count"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="添加"
android:onClick="insert"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="test"
android:onClick="test"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
新建MyopenHelper.java 类,继承SQLiteOpenHelper语句:代码如下:
public class MyopenHelper extends SQLiteOpenHelper{
private Context context;
private String dbname;//数据库名
public MyopenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { //构造器
super(context, name, factory, version);
this.context=context;
this.dbname=name;
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table books(id integer primary key autoincrement ,name text,count integer)"; //创建表
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
创建一个新的Activity,命名为 BookShowActivity,
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class BookShowActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_show);
MyopenHelper helper=new MyopenHelper(this,"BookStore.db",null,1);
SQLiteDatabase readableDatabase = helper.getReadableDatabase(); //读数据
String sql="select id,name,count from books"; //从book表里读取数据
Cursor c=readableDatabase.rawQuery(sql,null);
List<Book> books=new ArrayList<>(); //集合 将数据装到集合里
while (c.moveToNext()){
Book book=new Book();
book.setId(c.getInt(0));
book.setName(c.getString(1));
book.setCount(c.getInt(2));
books.add(book);
}
ListView lv=findViewById(R.id.lv); //实例化
ArrayAdapter<Book> adapter= new MyAdapter(this,R.layout.book_layout,books); //创建适配器,构造函数中把要适配的数据传入 ,其泛型指定为Book。
lv.setAdapter(adapter); //把构建好的适配器对象传递进去,这样ListView和数据之间的建立就建立完成。
}
}
新建一个Book类
public class Book {
private int id;
private String name;
private int count;
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 getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
activity_book_show.XML 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lnjr.sqlit.BookShowActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
新建一个book_layout布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="horizontal">
<!--weigth 按照比例分配-->
<TextView
android:id="@+id/nameTV"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/countTV"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="30dp" />
</LinearLayout>
新建一个MyAdapter类继承ArrayAdapter 的Book泛型
public class MyAdapter extends ArrayAdapter <Book>{
Context context;
int resource;
List<Book> books;
public MyAdapter( Context context, int resource, List<Book> objects) {
super(context, resource, objects);
this.resource=resource;
this.context=context;
this.books=objects;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
convertView=LayoutInflater.from(context).inflate(resource,parent,false); //创建视图
TextView nameTV=convertView.findViewById(R.id.nameTV); //把数据传进来,设置文本
TextView countTV=convertView.findViewById(R.id.countTV);
Book book=books.get(position);
nameTV.setText(book.getName());
countTV.setText(book.getCount()+"");
return convertView;
}
}