• SharedPreference-dan malumotlarni olish
  • NoteEditorActivity.java
  • SharedPreference-da qiymatlarni o'rnatish




    Download 0.5 Mb.
    bet3/5
    Sana07.06.2023
    Hajmi0.5 Mb.
    #70535
    1   2   3   4   5
    Bog'liq
    Jamoaviy ish 011-18 Rahimov M

    SharedPreference-da qiymatlarni o'rnatish:


    Общие рекомендации.Редактор редактор = getSharedPreferences(ИМЯ_ПРЕФИКСА, ИМЯ_МОДЕЛИ).редактировать();
    editor.putString(“имя”, “Елена”);
    editor.putInt(“idName”, 12);
    editor.apply();


    SharedPreference-dan ma'lumotlarni olish:


    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    // Значение по умолчанию не определено.
    Имя строки = prefs.getString(“имя”, “Имя не определено”);
    // 0 - значение по умолчанию.
    int idName = prefs.getInt(“idName”, 0);
    Quyida NoteEditorActivity.java faylining toʻliq kodi keltirilgan. Kodni batafsilroq tushunish uchun sharhlar kod ichiga qo'shiladi.


    Java
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.EditText;
    import androidx.appcompat.app.AppCompatActivity;
    import java.util.HashSet;


    public class NoteEditorActivity extends AppCompatActivity {
    int noteId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_editor);


    EditText editText = findViewById(R.id.editText);


    // Fetch data that is passed from MainActivity
    Intent intent = getIntent();


    // Accessing the data using key and value
    noteId = intent.getIntExtra("noteId", -1);
    if (noteId != -1) {
    editText.setText(MainActivity.notes.get(noteId));
    } else {


    MainActivity.notes.add("");
    noteId = MainActivity.notes.size() - 1;
    MainActivity.arrayAdapter.notifyDataSetChanged();


    }


    editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    // add your code here
    }


    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    MainActivity.notes.set(noteId, String.valueOf(charSequence));
    MainActivity.arrayAdapter.notifyDataSetChanged();
    // Creating Object of SharedPreferences to store data in the phone
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.notes", Context.MODE_PRIVATE);
    HashSet set = new HashSet(MainActivity.notes);
    sharedPreferences.edit().putStringSet("notes", set).apply();
    }


    @Override
    public void afterTextChanged(Editable editable) {
    // add your code here
    }
    });
    }
    }



    Download 0.5 Mb.
    1   2   3   4   5




    Download 0.5 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    SharedPreference-da qiymatlarni o'rnatish

    Download 0.5 Mb.