BÀI 31 - LƯU GIÁ TRỊ BIẾN (Shared Preferences) TRONG LẬP TRÌNH ANDROID
Video bài học:
Biến toàn cục SharedPreferences tên_biến_1;
OnCreate(){
tên biến 1 = getSharePreferences("tên biến xml", MODE_PRIVATE );
//Lưu biến vào
SharedPerferences.Editor tên_biến _2 = tên_biến_1.edit();
tên_biến_2.putString("tên biến",giá trị);
tên_biến_2.remove("tên biến"); //trường hợp else
tên_biến_2.commit(); //Xác nhận hoàn thành
// Lấy biến ra
gì đó.setText(tên biến 1.getString("tên biến","chuỗi mặc định"));
}
Chuyển arrayList sang array để làm chức năng lưu và autoCompleteTextView
arrayListAutoText = new ArrayList<>();
arrayListAutoText .add("Đội");
arrayListAutoText .add("Team");
arrayListAutoText .add("Giám khảo");
arrayListAutoText .add("judgus");
arrayText = arrayListAutoText.toArray(new String[0]); //arrayList.toArray(new String[arrayList.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this
, R.layout.support_simple_spinner_dropdown_item, arrayText);
editText.setAdapter(adapter);
editText.setThreshold(1);
button.setOnClickListener(v -> {
String Text = editText.getText().toString();
if (!Text.equals("")) {
arrayListAutoText.add(Text);
arrayText = arrayListAutoText.toArray(new String[0]);
adapter.notifyDataSetChanged();
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
arrayText = arrayListAutoText.toArray(new String[0]);
ArrayAdapter<String> adapterAutoFind
= new ArrayAdapter<>(Activity.this
, R.layout.support_simple_spinner_dropdown_item, arrayAutoText);
editText.setAdapter(adapter);
editText.setThreshold(1);
}
@Override
public void afterTextChanged(Editable s) {
}
});