鱼C论坛

 找回密码
 立即注册
查看: 2355|回复: 0

[学习笔记] android programing 6.4.3

[复制链接]
发表于 2017-10-18 10:37:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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="match_parent"
  5.     android:orientation="vertical">

  6.     <Button
  7.         android:id="@+id/create_database"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="create database"/>
  11.     <Button
  12.         android:id="@+id/add_data"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:text="Add data"/>

  16. </LinearLayout>
复制代码

MainActivy.java修改为:
  1. package com.example.xinwei.databasetest;

  2. import android.content.ContentValues;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;

  9. public class MainActivity extends AppCompatActivity {
  10.     private MyDatabaseHelper dbHelper;
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
  16.         Button createDatabase = (Button)findViewById(R.id.create_database);
  17.         createDatabase.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View view) {
  20.                 dbHelper.getWritableDatabase();
  21.             }
  22.         });
  23.         addData();
  24.     }

  25.     private void addData() {
  26.         Button addData = (Button)findViewById(R.id.add_data);
  27.         addData.setOnClickListener(new View.OnClickListener() {
  28.             @Override
  29.             public void onClick(View view) {
  30.                 SQLiteDatabase db = dbHelper.getWritableDatabase();
  31.                 ContentValues values=new ContentValues();
  32.                 values.put("name","The Da Vinci Code");
  33.                 values.put("author","Dan Brown");
  34.                 values.put("pages",454);
  35.                 values.put("price",16.96);
  36.                 db.insert("book",null,values);
  37.                 values.clear();
  38.                 values.put("name","The Lost Temple");
  39.                 values.put("author","Dan Brown");
  40.                 values.put("pages",510);
  41.                 values.put("price",19.95);
  42.                 db.insert("book",null,values);
  43.                 values.clear();
  44.             }
  45.         });
  46.     }
  47. }
复制代码

addData()这个函数里就是添加数据的方法。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 16:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表