SQLite连接操作
package com.kane.interviewcollection.dbc;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SqliteConnection extends SQLiteOpenHelper {
private static final int DBVERSION=1;
private static final String DBNAME="question.db";
//简化构造方法,哪个类需要调用数据库,就传参进来
public SqliteConnection(Context ctx){
super(ctx,DBNAME, null,DBVERSION);
}
public SqliteConnection(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
//安装时只建立一次,以后不执行了,主键id默认自增长,当然也可以写autoIncrement
String sql="CREATE TABLE question("+
"id integer primary key," +
"question text not null," +
"answer text not null" +
")";
db.execSQL(sql);
}
/**
* 当检测与前一次创建数据库版本不一样时,先删除表再创建新表才回进来
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS question");
//先删除旧表,再创建新表,必须版本号比以前高
onCreate(db);
}
}
SQLite的CRUD操作
package com.kane.interviewcollection.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.database.Cursor;
public class QuesstionDAOUtils {
public static void insert(Map<String,Object> map) {
String sql="INSERT INTO question(question,answer) VALUES(?,?>)";
Globals.dbc.getWritableDatabase().execSQL(sql,new Object[]{
map.get("question"),map.get("answer")
});
}
public static void deleteAll() {
String sql="DELETE FROM question";
Globals.dbc.getWritableDatabase().execSQL(sql);
}
public static List<Map<String,Object>> listDataPage (int pageNo,int pageSize,String keyword) {
List<Map<String,Object>> allValues=new ArrayList<Map<String,Object>>();
String sql = "SELECT id,question,answer FROM question WHERE question LIKE ? LIMIT ?,?";
Cursor c = Globals.dbc.getReadableDatabase().rawQuery(
sql,
new String[] { "%" + keyword + "%",
(pageNo - 1) * pageSize + "", String.valueOf(pageSize) });
c.moveToFirst();
while (!c.isAfterLast()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", c.getInt(0));
map.put("question", c.getInt(0) + "、" + c.getString(1));
map.put("answer", c.getString(2));
map.put("showFlag",false);//默认不显示答案,只有点击的显示答案
allValues.add(map);
c.moveToNext();
}
c.close();
return allValues;
}
public static int getAllCount(String keyword) {
String sql="SELECT COUNT(*) FROM question WHERE question LIKE ?";
Cursor c=Globals.dbc.getReadableDatabase().rawQuery(sql,
new String[]{"%"+keyword+"%"} );
c.moveToFirst();
int count =c.getInt(0);
c.close();
return count;
}
}
公共初始类
package com.kane.interviewcollection.util;
import com.kane.interviewcollection.dbc.SqliteConnection;
import android.app.Activity;
public class Globals {
public static int SCREEN_WIDTH;
public static int SCERRN_HEIGHT;
public static SqliteConnection dbc;
public static void init(Activity a){
dbc=new SqliteConnection(a);//对应需要的activity类
SCERRN_HEIGHT=a.getWindowManager().getDefaultDisplay().getHeight();
SCREEN_WIDTH=a.getWindowManager().getDefaultDisplay().getWidth();
}
}
}
主要界面代码
封面
<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"
android:background="@drawable/fengmian"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:id="@+id/verson_text"
android:text="needkane production \n 2013-04-23"/>
</RelativeLayout>
主界面
<LinearLayout 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"
android:background="@drawable/home_bg"
android:orientation="vertical" >
<!-- 提出公共界面 -->
<include layout="@layout/topbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/home_book_icon"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="欢迎使用"
android:gravity="center"
android:textColor="#ff0000"
android:textSize="30sp"
android:layout_weight="3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.7"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:background="@drawable/home_main"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.6"/>
</LinearLayout>
<include layout="@layout/bottombar"/>
</LinearLayout>
问题列表界面
<LinearLayout 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"
android:background="@drawable/home_bg"
android:orientation="vertical" >
<!-- 提出公共界面 -->
<include layout="@layout/topbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/middle_content_bg"
android:cacheColorHint="#00000000"
android:divider="#cccccc"
android:dividerHeight="1dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:scrollbars="none" >
<!-- android:divider="#cccccc"
android:dividerHeight="1dp"设置分割线 -->
</ListView>
</LinearLayout>
<include layout="@layout/bottombar" />
</LinearLayout>
头部界面
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/top_bg"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingRight="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_vertical"
android:text="程序员面试宝典"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/settingBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/top_setting" />
</LinearLayout>
尾部界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.7"
android:background="@drawable/bottom_toolbar"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<Button
android:id="@+id/bottom01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bottom_icon01_b" />
<Button
android:id="@+id/bottom02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bottom_icon02_a" />
<Button
android:id="@+id/bottom03"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bottom_icon03_a" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
浮动框界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="@+id/show_version"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="查看基本版本"
android:textColor="#ffffff"
android:textSize="14sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff" />
<TextView
android:id="@+id/about_author"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="关于作者"
android:textColor="#ffffff"
android:textSize="14sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff" />
<TextView
android:id="@+id/exit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="退出"
android:textColor="#ffffff"
android:textSize="14sp" />
</LinearLayout>