1. 使用Preference来保存首选项数据
在res/xml下建preference的xml文件
- <? xml version = "1.0" encoding = "utf-8" ?>
- <PreferenceScreen xmlns:android = "http://schemas.android.com/apk/res/android" >
- <PreferenceCategory android:title = "Category One" >
- <CheckBoxPreference
- android:defaultValue = "false"
- android:key = "checkboxpref"
- android:summary = "True or False"
- android:title = "CheckBox" />
- </PreferenceCategory>
- <PreferenceCategory android:title = "Category Two" >
- <EditTextPreference
- android:defaultValue = "[Enter a string here]"
- android:key = "editTextPref"
- android:summary = "Enter a string"
- android:title = "Edit Text" />
- <RingtonePreference
- android:key = "ringtonepref"
- android:summary = "select a ringtone"
- android:title = "Ringtones" />
- <PreferenceScreen
- android:key = "SecondPrefScreen"
- android:summary = "Click here to go to the second preference screen"
- android:title = "Second prefernce Screen" >
- <EditTextPreference
- android:key = "secondedittext"
- android:summary = "Enter a key"
- android:title = "Edit Text(Second Screen)" />
-
- </PreferenceScreen>
- </PreferenceCategory>
- </PreferenceScreen>
- public class MyPrefAty extends PreferenceActivity {
-
- @Override
- protected void onCreate ( Bundle savedInstanceState ) {
- super . onCreate ( savedInstanceState );
-
- PreferenceManager preferenceManager = getPreferenceManager ();
- preferenceManager . setSharedPreferencesName ( "MySelfdefinePrefName" );
-
- addPreferencesFromResource ( R . xml . myapppreferences );
- }
- }
- public void onClickDisplay ( View view ) {
- // SharedPreferences sharedPreferences = getSharedPreferences("com.example.administrator.mypreference_preferences", MODE_PRIVATE);
- SharedPreferences sharedPreferences = getSharedPreferences ( "MySelfdefinePrefName" , MODE_PRIVATE );
- Toast . makeText ( getBaseContext (), sharedPreferences . getString ( "editTextPref" , "" ), Toast . LENGTH_SHORT ). show ();
- }
-
- public void onClickModify ( View view ) {
- //SharedPreferences sharedPreferences = getSharedPreferences("com.example.administrator.mypreference_preferences", MODE_PRIVATE);
- SharedPreferences sharedPreferences = getSharedPreferences ( "MySelfdefinePrefName" , MODE_PRIVATE );
- SharedPreferences . Editor editor = sharedPreferences . edit ();
- editor . putString ( "editTextPref" , (( EditText ) findViewById ( R . id . txtString )). getText (). toString ());
- editor . commit ();
- }
2. 数据保存到内部存储
- public class FileSave extends Activity {
- EditText editText;
- static final int READ_BLOCK_SIZE = 100;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.myfilesavelayout);
- editText = (EditText) findViewById(R.id.edittextFile);
- }
- public void onClickLoad(View v) {
- try {
- FileInputStream fileInputStream = openFileInput("textfile.txt");
- InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
- char[] inputBuffer = new char[READ_BLOCK_SIZE];
- String s = "";
- int charRead;
- while ((charRead = inputStreamReader.read(inputBuffer)) > 0) {
- String readString = String.copyValueOf(inputBuffer, 0, charRead);
- s += readString;
- }
- editText.setText(s);
- Toast.makeText(getBaseContext(), "File Load Successfully.", Toast.LENGTH_SHORT).show();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void onClickSave(View view) {
- String string = editText.getText().toString();
- try {
- FileOutputStream fileOutputStream = openFileOutput("textfile.txt", MODE_WORLD_READABLE);
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
- outputStreamWriter.write(string);
- outputStreamWriter.flush();
- outputStreamWriter.close();
- Toast.makeText(getBaseContext(), "File Save Successfully", Toast.LENGTH_SHORT).show();
- editText.setText("");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
3. 保存数据到SD卡(外部存储)
- public class SdSave extends Activity {
- EditText editText;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mysdlayout);
- editText = (EditText) findViewById(R.id.SDedittextFile);
- }
- public void onClickSaveSD(View view) {
- String string = editText.getText().toString();
- try {
- File sdCard = Environment.getExternalStorageDirectory();
- File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
- directory.mkdir();
- File file = new File(directory, "textfile.txt");
- FileOutputStream fileOutputStream = new FileOutputStream(file);
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
- outputStreamWriter.write(string);
- outputStreamWriter.flush();
- outputStreamWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Toast.makeText(getBaseContext(), "Save In SD successfully!", Toast.LENGTH_SHORT).show();
- editText.setText("");
- }
- public void onClickLoadSD(View view) {
- try {
- File sdCard = Environment.getExternalStorageDirectory();
- File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
- File file = new File(directory, "textfile.txt");
- FileInputStream fileInputStream = new FileInputStream(file);
- InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
- char[] inputBuffer = new char[100];
- String s = "";
- int charRead;
- while ((charRead = inputStreamReader.read(inputBuffer)) > 0) {
- String readString = String.copyValueOf(inputBuffer, 0, charRead);
- s += readString;
- inputBuffer = new char[100];
- }
- editText.setText(s);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
4.使用数据库
- public class DBAdapter {
- static final String KEY_ROWID = "_id";
- static final String KEY_NAME = "name";
- static final String KEY_EMAIL = "email";
- static final String TAG = "DBAdapter";
-
- static final String DATABASE_NAME = "MyDB3";
- static final String DATABASE_TABLE = "contacts";
- static final int DATABASE_VERSON = 1;
-
- static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement,name text not null,email text not null);";
-
- final Context context;
-
- DatabaseHelper DBHelper;
-
- SQLiteDatabase db;
-
- public DBAdapter(Context context){
- this.context = context;
- DBHelper = new DatabaseHelper(context);
- }
-
- private static class DatabaseHelper extends SQLiteOpenHelper{
-
- DatabaseHelper(Context context){
- super(context, DATABASE_NAME, null, DATABASE_VERSON);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(DATABASE_CREATE);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w(TAG,"Upgrading database from version "+oldVersion+" to "+ newVersion + ",which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS contacts");
- onCreate(db);
- }
-
-
- }
-
-
- public DBAdapter open() throws SQLException{
- db = DBHelper.getWritableDatabase();
- //db = DBHelper.getReadableDatabase();
- return this;
- }
-
- public void close(){
- DBHelper.close();
- }
-
- public long inserContacts(String name,String email){
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_NAME,name);
- initialValues.put(KEY_EMAIL, email);
- return db.insert(DATABASE_TABLE,null,initialValues);
- }
-
- public boolean deleteContact(long rowId){
- return db.delete(DATABASE_TABLE,KEY_ROWID+"="+rowId,null)>0;
- }
-
- public Cursor getAllContacts(){
- return db.query(DATABASE_TABLE,new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL},null,null,null,null,null);
- }
-
- public Cursor getContact(long rowId) throws SQLException{
- Cursor cursor = db.query(true,DATABASE_TABLE,new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL},KEY_ROWID +"="+rowId,null,null,null,null,null);
- if (cursor!=null)
- cursor.moveToFirst();
- return cursor;
- }
-
- public boolean updateContact(long rowId,String name,String email){
- ContentValues args = new ContentValues();
- args.put(KEY_NAME,name);
- args.put(KEY_EMAIL,email);
- return db.update(DATABASE_TABLE,args,KEY_ROWID+"="+rowId,null)>0;
- }
- }
编程方式使用数据库——
- public class MainActivity extends Activity {
- DBAdapter dbAdapter;
- TextView textView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- textView = (TextView) findViewById(R.id.showInfo);
- dbAdapter = new DBAdapter(this);
-
- try {
- dbAdapter.open();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- long id = dbAdapter.inserContacts("Li XiaoMing","997654321@qq.com");
- id = dbAdapter.inserContacts("Bruce","lixiaoming@gmail.com");
- dbAdapter.close();
-
- }
-
- public void ShowAllcontacts(View view) {
- try {
- dbAdapter.open();
- Cursor cursor = dbAdapter.getAllContacts();
- String s = "";
-
- if (cursor.moveToFirst()) {
- do {
- s += "id: " + cursor.getString(0) + " Name: " + cursor.getString(1) + " Email: " + cursor.getString(2) + "\n";
- } while (cursor.moveToNext());
-
- textView.setText(s);
- }
- dbAdapter.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- public void ShowOneContact(View view) {
-
- EditText editText = (EditText) findViewById(R.id.edittext_id);
- int id = Integer.parseInt(editText.getText().toString());
- // Toast.makeText(getBaseContext(),Integer.toString(id),Toast.LENGTH_SHORT).show();
-
- try {
- dbAdapter.open();
- Cursor cursor = dbAdapter.getContact(id);
- if (cursor.moveToFirst())
- textView.setText("id: " + cursor.getString(0) + " Name: " + cursor.getString(1) + " Email: " + cursor.getString(2) + "\n");
- else
- Toast.makeText(getBaseContext(), "Contact Not Found", Toast.LENGTH_SHORT).show();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- dbAdapter.close();
-
- }
-
- public void UpdateContact(View view) {
- EditText editText = (EditText) findViewById(R.id.edittext_id);
- int id = Integer.parseInt(editText.getText().toString());
-
- try {
- dbAdapter.open();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (dbAdapter.updateContact(id, "Charley", "12332@outook.com"))
- Toast.makeText(getBaseContext(), "update Success", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(getBaseContext(), "update failure", Toast.LENGTH_SHORT).show();
- dbAdapter.close();
-
- }
-
- public void DeleteContact(View view) {
- EditText editText = (EditText) findViewById(R.id.edittext_id);
- int id = Integer.parseInt(editText.getText().toString());
-
- try {
- dbAdapter.open();
- } catch (SQLException e) {
- e.printStackTrace();
- }
-
- if (dbAdapter.deleteContact(id))
- Toast.makeText(getBaseContext(), "Delete Success", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(getBaseContext(), "Delete failure", Toast.LENGTH_SHORT).show();
-
- dbAdapter.close();
- }
- }
如果在程序中创建一个DB,导出后使用进行sqlitebrowser查看——发现,有2个额外的表 android_metadata和sqlite_squence
其中android_metadata用来定义使用的语言,可以删除(虽然有的博客说不行,但我亲测是可以的)
sqlite_squence似乎是来记录主要表的信息(项的数量),并且无法删除
代码:
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- textView = (TextView) findViewById(R.id.showInfo);
- dbAdapter = new DBAdapter(this);
-
- try {
- String destPath = "/data/data/" + getPackageName() + "/databases";
- Toast.makeText(getBaseContext(), destPath, Toast.LENGTH_SHORT).show();
- File file = new File(destPath);
- if (!file.exists()) {
- file.mkdirs();
- file.createNewFile();
- CopyDB(getBaseContext().getAssets().open("test2"), new FileOutputStream(destPath + "/MyDB3"));
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
-
- public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException {
- byte[] buffer = new byte[1024];
- int length;
- while ((length = inputStream.read(buffer)) > 0) {
- outputStream.write(buffer, 0, length);
- }
- Toast.makeText(getBaseContext(), "Copy Success", Toast.LENGTH_SHORT).show();
- inputStream.close();
- outputStream.close();
- }