android应用面试宝典(上);公共基类管理公共头尾页;公共activity的参数问题;SQLite存数据;前提先将txt数据保存在项目的assets文件下;

本文详细介绍了SQLite数据库的连接操作及CRUD(增删改查)功能,包括创建数据库、表,以及插入、删除、查询数据的方法。同时,展示了如何在Android应用中集成并使用SQLite数据库,以实现数据持久化存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值