主类
package com.zhl.test;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText ed;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通过传着四个参数创建数据库
Sql sql = new Sql(MainActivity.this, "name", null, 1);
db = sql.getWritableDatabase();
ed = (EditText) findViewById(R.id.name);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// 把从EditText中获取的值转成字符串
String str = ed.getText().toString();
// 同过ContentValues向数据库中添加数据
ContentValues cv = new ContentValues();
cv.put("name", str);
// 把数据添加到数据裤中
db.insert("name", null, cv);
startActivity(new Intent(MainActivity.this, ZhanShi.class));
}
});
}
}
布局
<RelativeLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
xmlns:tools="<a target=_blank href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:hint="请输入要存储的名字" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="71dp"
android:text="展示存储的名字" />
</RelativeLayout>
数据库类
package com.zhl.test;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Sql extends SQLiteOpenHelper {
public Sql(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
/**
* 创建数据库中的表
* */
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// 创建表时使用正常的sql语句进行创建
db.execSQL("create table name(id integer primary key autoincrement,name char)");
}
/**
* 数据库的版本发生改变时使用的方法
* */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
展示类
package com.zhl.test;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ZhanShi extends Activity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhan_shi);
Sql sql = new Sql(ZhanShi.this, "name", null, 1);
db = sql.getReadableDatabase();
// 创建查询语句
Cursor cr = db.query("name", null, null, null, null, null, null);
List<String> list = new ArrayList<String>();
while (cr.moveToNext()) {
// 查询表中摸个名字的值
String name = cr.getString(cr.getColumnIndex("name"));
// 放到集合中去
list.add(name);
}
ListView listview = (ListView) findViewById(R.id.listview);
// 使用listview展示出来
listview.setAdapter(new ArrayAdapter<String>(ZhanShi.this,
android.R.layout.simple_list_item_1, android.R.id.text1, list));
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ZhanShi" >
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
|