MainActivity代码如下:
package example.com.myapplication;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity
{
SQLiteDatabase db;
Button bn = null;
ListView listView;
String TAG = "TAG";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建或打开数据库(此处需要使用绝对路径)
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir()
.toString() + "/test.db3" , null);
Log.i(TAG, "onCreate: "+ this.getFilesDir()
.toString());
listView = (ListView)findViewById(R.id.show);
bn = (Button)findViewById(R.id.ok);
bn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View source)
{
//获取用户输入
String title = ((EditText)findViewById(R.id.title))
.getText().toString();
String content = ((EditText)findViewById(R.id.content))
.getText().toString();
try
{
insertData(db , title , content);
Cursor cursor = db.rawQuery("select * from news_inf", null);
inflateList(cursor);
}
catch(SQLiteException se)
{
//执行DDL创建数据表
db.execSQL("create table news_inf(_id integer primary key autoincrement,"
+ " news_title varchar(50),"
+ " news_content varchar(255))");
//执行insert语句插入数据
insertData(db , title , content);
//执行查询
Cursor cursor = db.rawQuery("select * from news_inf", null);
inflateList(cursor);
}
}
});
}
private void insertData(SQLiteDatabase db
, String title , String content)
{
//执行插入语句
db.execSQL("insert into news_inf values(null , ? , ?)"
, new String[]{title , content});
}
private void inflateList(Cursor cursor)
{
//填充SimpleCursorAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this , R.layout.ine, cursor
, new String[]{"news_title" , "news_content"}
, new int[]{R.id.my_title , R.id.my_content});
//显示数据
listView.setAdapter(adapter);
}
@Override
public void onDestroy()
{
super.onDestroy();
//退出程序时关闭SQLiteDatabase
if (db != null && db.isOpen())
{
db.close();
}
}
}
activity_main布局文件的代码:
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent"
android:orientation="vertical">
<EditText
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="2"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="insert"/>
<ListView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
ListView的子项目的布局文件::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment">
<TextView
android:id="@+id/my_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/my_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>