1.數(shù)據(jù)庫讀寫
創(chuàng)新互聯(lián)公司是一家專業(yè)提供富寧企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為富寧眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
首先新建一個(gè)數(shù)據(jù)庫工具類繼承自SQLiteOpenHelper,然后在構(gòu)造方法中指定數(shù)據(jù)庫名稱
和版本號(hào)進(jìn)行初始化。代碼如下:
MyDataBase.java
package chzu.csci.pwn.androidquiz;
?
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
?
public class MyDataBase extends SQLiteOpenHelper {
?
????private static final String DB_NAME="2017211808.db";
????private static final int DB_VERSION=1;
?
????MyDataBase(Context context){
????????super(context,DB_NAME,null,DB_VERSION);
????}
?
????@Override
????public void onCreate(SQLiteDatabase db) {
????????db.execSQL(//第一次運(yùn)行時(shí)新建一個(gè)BANK表,并且插入初始數(shù)據(jù)
????????????????"CREATE TABLE BANK (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
????????????????????????+"TYPE TEXT,"
????????????????????????+"QUESTION TEXT,"
????????????????????????+"ANSWER TEXT);"
????????);
????????insertBank(db,"架構(gòu)","Android系統(tǒng)架構(gòu)自底向上第二層是?");
????????insertBank(db,"編程","Android App開發(fā)支持編程語言包括?");
????????insertBank(db,"服務(wù)","綁定服務(wù)使用什么方法?");
????}
?
????@Override
????public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
?
????}
?
????private static void insertBank(SQLiteDatabase db,String type,String question){
????????ContentValues value=new ContentValues();
????????value.put("TYPE",type);
????????value.put("QUESTION",question);
????????long result=db.insert("BANK",null,value);
????}
}
1.插入數(shù)據(jù)
調(diào)用SQLiteDatabase類的insert方法即可,代碼如下,具體調(diào)用可以看上面的MyDataBase.java:
ContentValues value=new ContentValues();
value.put("TYPE",type);//字段名和值
value.put("QUESTION",question);//字段名和值
long result=db.insert("BANK",null,value);
2.刪除數(shù)據(jù)
調(diào)用SQLiteDatabase類的delete方法即可,代碼如下:
SQLiteOpenHelper helper=new MyDataBase(this);
try{
?????SQLiteDatabase db=helper.getWritableDatabase();
?????//刪除TYPE字段為"架構(gòu)"的行
?????db.delete("BANK","TYPE=?",new String[]{"架構(gòu)"});
?}catch (SQLException e){
?????Toast.makeText(this,"DataBase unavalibale",Toast.LENGTH_SHORT).show();
?}
3.修改數(shù)據(jù)
調(diào)用SQLiteDatabase類的update方法,傳入ContentValues對(duì)象即可,代碼如下:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues bankvalues=new ContentValues();
bankvalues.put("ANSWER",answer);//ANSWER為想要修改的字段名,answer為想要修改成的值
db.update("BANK",bankvalues,"_id=?",new String[]{Integer.toString(ID)});
4.查詢數(shù)據(jù)
調(diào)用SQLiteDatabase類的query方法,返回一個(gè)Cursor對(duì)象,類似于ResultSet結(jié)果集,
代碼如下:
SQLiteDatabase db=helper.getReadableDatabase();
cursor= db.query("BANK",new String[]{"_id","TYPE"},null,null,null,null,null);//相當(dāng)于執(zhí)行SQL語句SELECT _id,TYPE FROM BANK
加上查詢條件,代碼如下:
SQLiteDatabase db=helper.getReadableDatabase();
cursor=db.query("BANK",new String[]{"QUESTION","ANSWER"},"_id=?",
?????????new String[]{Integer.toString(ID)},null,null,null);
?????????/*相當(dāng)于執(zhí)行SQL語句SELECT QUESTION,ANSWER FROM BANK WHERE _id=ID*/ ???????
2.按鍵事件處理
public void onClickSave(View view){
? /*此處填寫點(diǎn)擊后的響應(yīng)事件邏輯代碼,另外在視圖層對(duì)應(yīng)的xml文件中相應(yīng)按鈕的屬性設(shè)置android:onClick="onClickSave"*/ ??????
}
3.CursorAdapter(游標(biāo)適配器)
通過findViewById方法獲取ListView組件,然后再調(diào)用db的query方法獲取數(shù)據(jù)庫中的內(nèi)容,
返回值是一個(gè)Cursor游標(biāo)對(duì)象,對(duì)SimpleCursorAdapter類進(jìn)行構(gòu)造后生成對(duì)象后,調(diào)用
ListView對(duì)象的setAdapter方法,進(jìn)行適配器設(shè)置,即可在頁面中顯示數(shù)據(jù)庫內(nèi)容。
/*listView為響應(yīng)的ListView組件對(duì)象,cursor為查詢出的的游標(biāo)對(duì)象,new String{"TYPE"}是在ListView中展示的內(nèi)容
*/
SimpleCursorAdapter listAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,
????????????????????cursor,new String[]{"TYPE"},new int[]{android.R.id.text1},0);
listView.setAdapter(listAdapter);
4.界面跳轉(zhuǎn)時(shí)傳遞數(shù)據(jù)
通過構(gòu)造一個(gè)Intent對(duì)象,然后調(diào)用intent對(duì)象的putExtra方法存入數(shù)據(jù),最后調(diào)
用startActivity方法啟動(dòng)。
/*MainActivity是跳轉(zhuǎn)前的界面Activity,SecondActivity是跳轉(zhuǎn)的目標(biāo)界面的Activity*/
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("ID",(int)id);
startActivity(intent);
5.Toast會(huì)話
/*"DataBase unavalibale為顯示的內(nèi)容,Toast.LENGTH_SHORT為會(huì)話持續(xù)時(shí)間,改為Toast.LENGTH_LONG可以延長會(huì)話持續(xù)時(shí)間*/
Toast.makeText(this,"DataBase unavalibale",Toast.LENGTH_SHORT).show();
6.音樂播放服務(wù)
通過綁定式服務(wù)即可實(shí)現(xiàn),在res目錄下新建一個(gè)raw目錄,然后將音樂文件粘貼進(jìn)該目錄,具體代碼如下:
MusicService.java
package chzu.csci.pwn.androidquiz;
?
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
?
import androidx.annotation.Nullable;
?
public class MusicService extends Service{
?
????private MediaPlayer mediaPlayer;
?
????//Service初始化時(shí)調(diào)用
????public void onCreate(){
????????super.onCreate();
????????mediaPlayer = MediaPlayer.create(this, R.raw.notice);//這里注意將音樂文件修改成自己的
????}
?
????public class MusicBinder extends Binder{
????????MusicService getMusic(){
????????????return MusicService.this;
????????}
????}
?
????private IBinder binder=new MusicBinder();
?
????@Override
????public IBinder onBind(Intent intent) {
????????if(!mediaPlayer.isPlaying()){
????????????// 開始播放
????????????mediaPlayer.start();
????????????// 允許循環(huán)播放
????????????mediaPlayer.setLooping(true);
????????}
????????return binder;
????}
?
????public boolean onUnbind(Intent intent) {
????????if(mediaPlayer.isPlaying()){
????????????mediaPlayer.stop();
????????}
????????return super.onUnbind(intent);
????}
?
????@Override
????public void onDestroy() {
????????// TODO Auto-generated method stub
????????super.onDestroy();
????????//先停止 再釋放
????????if(mediaPlayer.isPlaying()){
????????????mediaPlayer.stop();
????????}
????????mediaPlayer.release();
?
????}
????public MusicService() {
????}
}
onClickStart方法調(diào)用后播放音樂,onClickStop方法調(diào)用后停止播放,可以將方法與按鈕的onClick進(jìn)行綁定,從而通過按鈕控制音樂開關(guān)。
MainActivity.java
/*導(dǎo)入類代碼省略*/
public class MainActivity extends AppCompatActivity {
?
????/*無關(guān)代碼省略*/
????private MusicService musicService;
????private ServiceConnection connection;
?
????@Override
????protected void onCreate(Bundle savedInstanceState) {
???????/*無關(guān)代碼省略*/
????????connection=new ServiceConnection() {
????????function(){ //KDJ指標(biāo)http://www.fx61.com/faq/muniu/456.html
????????????@Override
????????????public void onServiceConnected(ComponentName name, IBinder service) {
????????????????MusicService.MusicBinder binder=(MusicService.MusicBinder)service;
????????????????musicService=binder.getMusic();
????????????}
?
????????????@Override
????????????public void onServiceDisconnected(ComponentName name) { }
????????};
???????/*無關(guān)代碼省略*/
????}
?
????/*無關(guān)代碼省略*/
????
????public void onClickStart(View view){
???????/*無關(guān)代碼省略*/
????????Intent intent = new Intent(this,MusicService.class);
????????bindService(intent,connection,BIND_AUTO_CREATE);
????}
?
????public void onClickStop(View view){
??????/*無關(guān)代碼省略*/
???????unbindService(connection); ????
????}
}
?
7.橫豎屏切換處理
主要就是將Activity的某些變量在Activity退出調(diào)用onSaveInstanceState時(shí)進(jìn)行值儲(chǔ)存,然后在載入Activity調(diào)用onCreate方法時(shí)進(jìn)行值讀取,相關(guān)代碼如下:
MainActivity.java
/*無關(guān)代碼已省略*/
public class MainActivity extends AppCompatActivity {
?
????private boolean running=false;
????private int seconds=0;
????private boolean wasRunning=false;
????
????@Override
????protected void onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_main);
????????if(savedInstanceState!=null){
????????????seconds=savedInstanceState.getInt("seconds");
????????????running=savedInstanceState.getBoolean("running");
????????????wasRunning=savedInstanceState.getBoolean("wasRunning");
????????}
????}
?
????@Override
????protected void onStop() {
????????super.onStop();
????????wasRunning=running;
????????running=false;
????}
?
????@Override
????protected void onStart() {
????????super.onStart();
????????if(wasRunning){
????????????running=true;
????????}
????}
?
????@Override
????public void onSaveInstanceState(Bundle outState) {
????????super.onSaveInstanceState(outState);
????????outState.putBoolean("running",running);
????????outState.putInt("seconds",seconds);
????????outState.putBoolean("running",wasRunning);
????}
?
}
?
8.線程
一些耗時(shí)操作可以用線程操作,以免堵塞??梢月暶饕粋€(gè)內(nèi)部類繼承自AsyncTask,然后
重寫其onPreExecute、doInBackground、onPostExecute方法。調(diào)用時(shí)調(diào)用該內(nèi)部類對(duì)象
的execute方法即可,注意drinkId與AsyncTask<Integer,Void,Boolean>中的Integer相對(duì)應(yīng)。
/*無關(guān)代碼已省略*/
public class DrinkActivity extends AppCompatActivity {
?
????public void onFavoriteClicked(View view){
????????int drinkId=getIntent().getIntExtra(EXTRA_DRINKID,0);
????????new UpdateDrinkTask().execute(drinkId);
????}
?
????private class UpdateDrinkTask extends AsyncTask<Integer,Void,Boolean>{
?
????????private ContentValues drinkvalues;
????????@Override
????????protected void onPreExecute() {
???????? /*執(zhí)行初始化任務(wù),可以在此進(jìn)行一些變量的值的初始化*/
????????????CheckBox favorite=(CheckBox)findViewById(R.id.favorite);
????????????drinkvalues=new ContentValues();
????????????drinkvalues.put("FAVORITE",favorite.isChecked());
????????}
????????@Override
????????protected Boolean doInBackground(Integer... integers) {
????????????/*執(zhí)行費(fèi)時(shí)任務(wù),返回一個(gè)boolean值,可以在此執(zhí)行一些數(shù)據(jù)庫等費(fèi)時(shí)操作*/
????????????int drinkId=getIntent().getIntExtra(EXTRA_DRINKID,0);
????????????SQLiteOpenHelper helper=new StarbuzzDatabaseHelper(DrinkActivity.this);
????????????try{
????????????????SQLiteDatabase db=helper.getWritableDatabase();
????????????????db.update("DRINK",drinkvalues,"_id=?",new String[]{Integer.toString(drinkId)});
????????????????return true;
????????????}catch (SQLException e){
????????????????return false;
????????????}
????????}
????????@Override
????????protected void onPostExecute(Boolean aBoolean) {
???????????/*代碼*/
????????}
????}
}
?
?
新聞名稱:Android復(fù)習(xí)知識(shí)點(diǎn)
地址分享:http://jinyejixie.com/article44/gpiohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站設(shè)計(jì)、、云服務(wù)器、網(wǎng)站排名、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)