91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android復習知識點

發布時間:2020-02-29 14:38:03 來源:網絡 閱讀:149 作者:專注地一哥 欄目:移動開發

1.數據庫讀寫

首先新建一個數據庫工具類繼承自SQLiteOpenHelper,然后在構造方法中指定數據庫名稱

和版本號進行初始化。代碼如下:

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(//第一次運行時新建一個BANK表,并且插入初始數據

????????????????"CREATE TABLE BANK (_id INTEGER PRIMARY KEY AUTOINCREMENT,"

????????????????????????+"TYPE TEXT,"

????????????????????????+"QUESTION TEXT,"

????????????????????????+"ANSWER TEXT);"

????????);

????????insertBank(db,"架構","Android系統架構自底向上第二層是?");

????????insertBank(db,"編程","Android App開發支持編程語言包括?");

????????insertBank(db,"服務","綁定服務使用什么方法?");

????}

?

????@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.插入數據

調用SQLiteDatabase類的insert方法即可,代碼如下,具體調用可以看上面的MyDataBase.java

ContentValues value=new ContentValues();

value.put("TYPE",type);//字段名和值

value.put("QUESTION",question);//字段名和值

long result=db.insert("BANK",null,value);

2.刪除數據

調用SQLiteDatabase類的delete方法即可,代碼如下:

SQLiteOpenHelper helper=new MyDataBase(this);

try{

?????SQLiteDatabase db=helper.getWritableDatabase();

?????//刪除TYPE字段為"架構"的行

?????db.delete("BANK","TYPE=?",new String[]{"架構"});

?}catch (SQLException e){

?????Toast.makeText(this,"DataBase unavalibale",Toast.LENGTH_SHORT).show();

?}

3.修改數據

調用SQLiteDatabase類的update方法,傳入ContentValues對象即可,代碼如下:

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.查詢數據

調用SQLiteDatabase類的query方法,返回一個Cursor對象,類似于ResultSet結果集,

代碼如下:

SQLiteDatabase db=helper.getReadableDatabase();

cursor= db.query("BANK",new String[]{"_id","TYPE"},null,null,null,null,null);//相當于執行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);

?????????/*相當于執行SQL語句SELECT QUESTION,ANSWER FROM BANK WHERE _id=ID*/ ???????

2.按鍵事件處理

public void onClickSave(View view){

? /*此處填寫點擊后的響應事件邏輯代碼,另外在視圖層對應的xml文件中相應按鈕的屬性設置android:onClick="onClickSave"*/ ??????

}

3.CursorAdapter(游標適配器)

通過findViewById方法獲取ListView組件,然后再調用dbquery方法獲取數據庫中的內容,

返回值是一個Cursor游標對象,對SimpleCursorAdapter類進行構造后生成對象后,調用

ListView對象的setAdapter方法,進行適配器設置,即可在頁面中顯示數據庫內容。

/*listView為響應的ListView組件對象,cursor為查詢出的的游標對象,new String{"TYPE"}是在ListView中展示的內容

*/

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.界面跳轉時傳遞數據

通過構造一個Intent對象,然后調用intent對象的putExtra方法存入數據,最后調

startActivity方法啟動。

/*MainActivity是跳轉前的界面ActivitySecondActivity是跳轉的目標界面的Activity*/

Intent intent=new Intent(MainActivity.this,SecondActivity.class);

intent.putExtra("ID",(int)id);

startActivity(intent);

5.Toast會話

/*"DataBase unavalibale為顯示的內容,Toast.LENGTH_SHORT為會話持續時間,改為Toast.LENGTH_LONG可以延長會話持續時間*/

Toast.makeText(this,"DataBase unavalibale",Toast.LENGTH_SHORT).show();

6.音樂播放服務

通過綁定式服務即可實現,在res目錄下新建一個raw目錄,然后將音樂文件粘貼進該目錄,具體代碼如下:

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初始化時調用

????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();

????????????// 允許循環播放

????????????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方法調用后播放音樂,onClickStop方法調用后停止播放,可以將方法與按鈕的onClick進行綁定,從而通過按鈕控制音樂開關。

MainActivity.java

/*導入類代碼省略*/

public class MainActivity extends AppCompatActivity {

?

????/*無關代碼省略*/

????private MusicService musicService;

????private ServiceConnection connection;

?

????@Override

????protected void onCreate(Bundle savedInstanceState) {

???????/*無關代碼省略*/

????????connection=new ServiceConnection() {

????????function(){ //KDJ指標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) { }

????????};

???????/*無關代碼省略*/

????}

?

????/*無關代碼省略*/

????

????public void onClickStart(View view){

???????/*無關代碼省略*/

????????Intent intent = new Intent(this,MusicService.class);

????????bindService(intent,connection,BIND_AUTO_CREATE);

????}

?

????public void onClickStop(View view){

??????/*無關代碼省略*/

???????unbindService(connection); ????

????}

}

?

7.橫豎屏切換處理

主要就是將Activity的某些變量在Activity退出調用onSaveInstanceState時進行值儲存,然后在載入Activity調用onCreate方法時進行值讀取,相關代碼如下:

MainActivity.java

/*無關代碼已省略*/

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.線程

一些耗時操作可以用線程操作,以免堵塞。可以聲明一個內部類繼承自AsyncTask,然后

重寫其onPreExecutedoInBackgroundonPostExecute方法。調用時調用該內部類對象

execute方法即可,注意drinkIdAsyncTask<Integer,Void,Boolean>中的Integer相對應。

/*無關代碼已省略*/

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() {

???????? /*執行初始化任務,可以在此進行一些變量的值的初始化*/

????????????CheckBox favorite=(CheckBox)findViewById(R.id.favorite);

????????????drinkvalues=new ContentValues();

????????????drinkvalues.put("FAVORITE",favorite.isChecked());

????????}

????????@Override

????????protected Boolean doInBackground(Integer... integers) {

????????????/*執行費時任務,返回一個boolean值,可以在此執行一些數據庫等費時操作*/

????????????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) {

???????????/*代碼*/

????????}

????}

}

?

?


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

杭州市| 大足县| 阳山县| 黎川县| 五指山市| 长春市| 惠安县| 昆山市| 呈贡县| 泗水县| 高台县| 普陀区| 蓝山县| 富源县| 锡林郭勒盟| 金坛市| 武穴市| 孝昌县| 临高县| 弥勒县| 祁连县| 凉城县| 金湖县| 淮安市| 巴林左旗| 清水河县| 涿州市| 大关县| 宁津县| 抚远县| 保德县| 开阳县| 漳州市| 盐津县| 漯河市| 苏尼特左旗| 冀州市| 会同县| 佛教| 淮滨县| 吉隆县|