package demo.fu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class fu extends Activity {
/* 全局变量 */
private static String DATABASE_NAME = "ENEST";
private static int DATABASE_VERSION = 2;
public static String TABLE_NAME_ONE = "ONE";
public static String TITLE_ONE = "Title_one";
public static String BODY_ONE = "Body_one";
public static DatabaseHelper mOpenHelper;
private static int Num_0 = 0;
private static int Num_1 = 1;
private static int Num_2 = 2;
public static ArrayList<Map<String, Object>> arraylistmap;
public static Cursor myCursor_one;
public static String mytitle;
public static String mybody;
public static HashMap<String, Object> mymap;
/* 数据库HELPER类 */
static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
//打开数据库 DATABASE_NAME,没有就新建。
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
//新建一个表。为了更直观,我放在下面了。
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOpenHelper = new DatabaseHelper(this);
arraylistmap = new ArrayList<Map<String, Object>>();
//获取编辑数据库的权限,使之具有读写能力。
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
//新建一个表~~~~之后可以不用改代码~~~~
/*** String sql = "CREATE TABLE " + TABLE_NAME_ONE + "(" + TITLE_ONE
+ " text not null, " + BODY_ONE + " text not null " + ");";
try {
db.execSQL("DROP TABLE IF EXISTS diary");
db.execSQL(sql);
setTitle("Table one create succeed");
} catch (SQLException e) {
setTitle("Table one create fault");
} ***/
// 向数据库里面插入数据三条
String sqlone = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ", "
+ BODY_ONE + ") values('" + Num_0 + "','[one]this is 0');";
db.execSQL(sqlone);
Log.e("table one ", "num 0 has insert");
String sqltwo = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ", "
+ BODY_ONE + ") values('" + Num_1 + "','[one]this is 1');";
db.execSQL(sqltwo);
Log.e("table one ", "num 1 has insert");
String sqlsh = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ", "
+ BODY_ONE + ") values('" + Num_2 + "','[one]this is 2');";
db.execSQL(sqlsh);
Log.e("table one ", "num 2 has insert");
//查询数据库里面的数据
myCursor_one = db.rawQuery("SELECT * FROM ONE", null);
int first = myCursor_one.getColumnIndex(TITLE_ONE);
int second = myCursor_one.getColumnIndex(BODY_ONE);
arraylistmap.clear();
if (myCursor_one.moveToFirst()) {
do {
//将数据绑定在arraylist数组里面
mytitle = myCursor_one.getString(first);
mybody = myCursor_one.getString(second);
mymap = new HashMap<String, Object>();
mymap.put(TITLE_ONE, mytitle);
mymap.put(BODY_ONE, mybody);
arraylistmap.add(mymap);
} while (myCursor_one.moveToNext());
}
//完了之后关闭读取的数据信息
myCursor_one.close();
//将arraylist数组里面的信息绑定到 listbiew上面
ListView listView = new ListView(this);
/*
* ADAPTER将ARRAYLISTMAP中的数据以COPY_MAIN布局文件的形式输出,TITLE_ONE数据对应LIST_TITLE,
* BODY_ONE数据对应到LIST_BODY
*/
SimpleAdapter adapter = new SimpleAdapter(this,
fu.arraylistmap, R.layout.copy_main, new String[] {
fu.TITLE_ONE, fu.BODY_ONE }, new int[] {
R.id.list_title, R.id.list_body });
listView.setAdapter(adapter);
/* 读取游标MYCURSOR_ONE读到的数据条目数,设置为TITLE */
int title = fu.myCursor_one.getCount();
setTitle("Count of Table One: " + title);
setContentView(listView);
}
}