X
    الأقسام: أخبار

تعلم معنا برمجة الأندرويد: 8- تخزين البيانات والملفات

:

.. .

.

:

1- Shared Preferences

-.

2- SQLite Databases

.

3- Internal Storage

.

4- External Storage

.

5- Network Connection

.

: SharedPreferences :

SharedPreferences / (key/value) Bundle .

xml .

Login .

SharedPreferences :

SharedPreferences getPreferences :

  • MODE_PRIVATE xml
  • MODE_WORLD_READABLE xml
  • MODE_WORLD_WRITEABLE xml

xml getSharedPreferences .

-object SharedPreferences Editor key/value xml put commit int, long, float, String & Boolean SharedPreferences

12345
SharedPreferences prefs = getSharedPreferences("myDataStorage", MODE_PRIVATE);Editor mEditor = prefs.edit();mEditor.putString("username","datastorageuser1");mEditor.putString("password","password1234");mEditor.commit();

123
SharedPreferences prefs = getSharedPreferences("myDataStorage", MODE_PRIVATE);String username = prefs.getString("username", "");String password = prefs.getString("password", "");

:

:

1- eclipse SharedPreferences

2- main.xml

123456789101112131415161718192021222324252627282930
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <SeekBarandroid:id="@+id/SeekBar01"android:layout_width="fill_parent"android:layout_height="wrap_content" /> <TextViewandroid:id="@+id/TextView01"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/welcome" /> <EditTextandroid:id="@+id/EditText01"android:layout_width="fill_parent"android:layout_height="wrap_content" /> <Buttonandroid:id="@+id/btnSave"android:text="Save"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </LinearLayout>

welcome

3- SharedPreferencesActivity

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
package com.androidhas.SharedPreferences; import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.Toast; public class SharedPreferencesActivity extends Activity { private SharedPreferences prefs;private String prefName = "MyPref";private EditText editText;private SeekBar seekBar;private Button btn; private static final String FONT_SIZE_KEY = "fontsize";private static final String TEXT_VALUE_KEY = "textvalue"; /** Called when the activity is first created. linkwheel creation . */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); editText = (EditText) findViewById(R.id.EditText01);seekBar = (SeekBar) findViewById(R.id.SeekBar01);btn = (Button) findViewById(R.id.btnSave); btn.setOnClickListener(new View.OnClickListener() {public void onClick(View v) { //---get the SharedPreferences object---prefs = getSharedPreferences(prefName, MODE_PRIVATE);prefs = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); //---save the values in the EditText view to preferences---editor.putFloat(FONT_SIZE_KEY, editText.getTextSize());editor.putString(TEXT_VALUE_KEY, editText.getText().toString()); //---saves the values---editor.commit(); //---display file saved message---Toast.makeText(getBaseContext(),"Font size saved successfully!",Toast.LENGTH_SHORT).show();}}); //---load the SharedPreferences object---SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);prefs = getPreferences(MODE_PRIVATE); //---set the TextView font size to the previously saved values---float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12); //---init the SeekBar and EditText---seekBar.setProgress((int) fontSize);editText.setText(prefs.getString(TEXT_VALUE_KEY, ""));editText.setTextSize(seekBar.getProgress()); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {} @Overridepublic void onStartTrackingTouch(SeekBar seekBar) {} @Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {//---change the font size of the EditText---editText.setTextSize(progress);}}); }}

4-

save .

: :

SQLite .

SQLiteOpenHelper override onCreate SQLite querying .

.

Leave a Comment