2020-11-10(安卓如何传递数据)
生活随笔
收集整理的這篇文章主要介紹了
2020-11-10(安卓如何传递数据)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.從文件讀取 保存文件的路徑使用api去獲取
a.getCacheDir();獲取/data/data/包名/Cache這個路徑 b.getFilesDir();獲取/data/data/包名/files這個路徑 c.openFileInput(“文件名”)操作getFiles()目錄下的文件,返回一個輸入流FileInutStream d.openFileOutput(“文件名”,mode)操作getFilesDir()目錄下的文件,返回一個輸出流 FileOutputStream e.Environment.gettexternalStorageDirectory()獲取sd卡路徑2.從sharedprefererced中獲取(只能存6種類型:int boolean long float String set)
獲取Sharepreferences對象
獲取sp的Editor對象,通過Editor保存用戶名和密碼
Editor edit=sp.edit() edit.putSting(“username”,username) edit.putSting(“pwd”,pwd) edit.putBoolean(“isSave”,true) 只有調(diào)用了commit提交之后修改才會生效 edit.commit()3.從數(shù)據(jù)庫中獲取
第一步 寫一個類創(chuàng)建SQLiteOpenHelper
第二步 創(chuàng)建SQLiteOpenHelper對象, 獲得sqliteDataBase通過
SQLiteDataBase執(zhí)行相應(yīng)的方法 public class Mainactivity extends Activity{ private MyOpenHelper openhelper; private SQLiteDatabase database;protected void onCreate(Bundle saveInstanceState) { super.onCreat(savedInstanceState); setContentenView(R.layout.activity_main); database=openHelper.getReadableDatabase(); databese=openHelper.getWriteableDatabase(); } public void insert(View v){ //insert into info(name,phone)values(“小濤”,“116463834838”) database.execSQL(insert into info(name,phone)values(“小濤”,“116463834838”))//這方法沒返回值,無法獲得執(zhí)行的狀態(tài)(但是下面谷歌封裝好的api都有返回值,相對來講,谷歌封裝效率差些,sql語句使用也更加的被限制) database.execSQL(insert into info(name,phone)values(“小劉”,“1123433834838”)) }public void updata(View v){ database.execSQL("update into set phone="1354343135"where name="小賈" ")); } public void delete(View v){ database.execSQL("delete from info where name="小賈" ")); }public void query(View v){ Cursor cursor=database.rawQuery(“select * from indo”,null) while(cursor.moveToNext()){ String temp=cursor.getString(cursor.getColumnIndex(“phone”)); //Log.d(“result”,“temp+”); for(int i=0;i<cursor.getColunmnCount();i++) {String result=cursor.getString(i); Log.d(“result”,“temp+”); } } database.close(); }public void insert1(View v){ ContentValues values=new ContentValues(); //key就是要插入的列的名字 value就是要插入的具體的值(列的索引是從0開始) values.put(“name”,“王五”); values.put(“phone”,“15555555555”); //insert into info (name,phone ) values(“王五”,“564464654”) //insert into info (name )values(null); //第一個參數(shù) 數(shù)據(jù)要插入的表的名字 //第二個參數(shù) String類型的參值,避免出現(xiàn)insert into info(name ) values(null);這種sql語句 //如果確定不會有問題可以傳null或者傳一個可以接受null值的列名 //第三個參數(shù),ContentValues內(nèi)部保存了一個map,通過key-value鍵值對的形式把要插入的數(shù)據(jù)封裝起來 //values.put(“name”,“王五”) long result=database.insert(“info”,“name”,values)(如果result 等于-1,則說明執(zhí)行語句失敗); } public void updata1(View v){ ContentValues values=new ContentValues(); //database.execSQL("update into set phone="1354343135"where name="小賈" ")); //第一個參數(shù)表名 //第二個參數(shù) 要修改的信息,以鍵值對的形式保存 /第三個參數(shù) where條件 name=? 注意where不用寫,只需要寫where后面的內(nèi)容 //第四個參數(shù) where條件中 ?具體的值,用String數(shù)組來保存,如果有多個條件,數(shù)組中的數(shù)據(jù)要對應(yīng)起來 //返回值 就是更改究竟影響到幾行數(shù)據(jù) int update=database.update(“info”,values,“value=?”,new String[ ]{"王五"}); Toast.makeText(this,“修改了”+updata+“行數(shù)據(jù)”,Toast.LENGTH_SHORT).show(); }public void delete1(View v){ //database.execSQL("delete from info where name="小賈" ")) //第一個參數(shù)表名 //第二個參數(shù),where條件,name=?,注意where不用寫,只需要寫where后面的內(nèi)容 //第三個參數(shù), where條件中 ?具體的值,用String數(shù)組來保存,如果有多個條件,數(shù)組中的數(shù)據(jù)要對應(yīng)起來 //返回值, 如果第二個和第三個參數(shù)不為空,則返回值為具體刪除的記錄數(shù),如果沒有傳第二個和第三個參數(shù),這說明要刪除全部的數(shù)據(jù),如果刪除成功返回 int delete=database.delete(“info”,“value=?”,new String[ ]{"王五"}); Toast.makeText(this,“刪除了”+delete+“條數(shù)據(jù)”,Toast.LENGTH_SHORT).show(); } public void query1(View v){ //database.execSQL("select name,phone,from info where name="小賈" ")) //第一個參數(shù)表名 //第二個參數(shù),要查詢的列名,用String數(shù)組來表示,如果是select *則傳入一個null //第三個參數(shù),要查詢的條件,where 語句,name=? //第四個參數(shù),條件語句的具體的值,用String數(shù)組來表示 //第五個參數(shù),groupBy分組查詢語句 //第六個參數(shù),groupBy的時候要使用的條件語句having //第七個條件,排序 Cursor cursor =database.query(“info”,null,null,null,null,null,null) while(cursor.moveToNext()) { String string=cursor.getSting(cursor.getColumnIndex(“name”)); Log.d(“API”,string) } cursor.close(); } protected void onDestroy(){ super.onDestroy(); database.close(); } }4.通過網(wǎng)絡(luò)獲取(通過網(wǎng)絡(luò)加載數(shù)據(jù)的代碼基本會寫在單獨的代碼中,避免oncreat方法代碼過多)
注:聯(lián)網(wǎng)操作需要開子線程,不能在主線程做聯(lián)網(wǎng)的操作(不能在子線程更新UI)
5.從上個activity中獲取
//找到intent獲取數(shù)據(jù)
//通過intent的getXXXExtra方法,獲取上個頁面?zhèn)鬟f的數(shù)據(jù)
String name=intent.getStringExtra(“name”); boolean inFemale =intent.getBooleanExtra(“isFemale”,false);總結(jié)
以上是生活随笔為你收集整理的2020-11-10(安卓如何传递数据)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020-11-5(安卓)
- 下一篇: 条件跳转指令总结