我可以将Stringset放在SharedPreferences中。但是当我试图获得回报时,我做不到。
我得到的设定尺寸为0
这是我保存====>
的代码公共类DefineProduct扩展了AppCompatActivity {
EditText productName, barcode,cost,price,size,color,notes;
Button saveBtn;
SharedPreferences sharedPref;
SharedPreferences.Editor editor ;
Set<String> set = new HashSet<>();
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_define_product);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPref.edit();
productName = findViewById(R.id.productNameText);
barcode = findViewById(R.id.barcodeEditText);
cost = findViewById(R.id.costText);
price = findViewById(R.id.priceText);
size = findViewById(R.id.sizeText);
color = findViewById(R.id.colorText);
notes = findViewById(R.id.notesText);
saveBtn = findViewById(R.id.saveBtn);
}
public void Save(View view){
set.add(productName.getText().toString());
set.add(barcode.getText().toString());
set.add(cost.getText().toString());
set.add(price.getText().toString());
set.add(size.getText().toString());
set.add(color.getText().toString());
set.add(notes.getText().toString());
editor.putStringSet(barcode.getText().toString(),set);
editor.commit();
Toast.makeText(getApplicationContext(),"Saved", Toast.LENGTH_LONG).show();
productName.setText("");
barcode.setText("");
cost.setText("");
price.setText("");
size.setText("");
color.setText("");
notes.setText("");
set.clear();
}
}
这是我从其他活动中获取StringSet的代码=====>
public void onActivityResult(int requestCode,int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if(islem == 1 && resultCode == RESULT_OK && data != null){
Uri uri = data.getData();
try {
if(Build.VERSION.SDK_INT >= 28){
ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), uri);
productImage = ImageDecoder.decodeBitmap(source);
productImageVeawer.setImageBitmap(productImage);
productImage.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
}
else {
productImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
productImageVeawer.setImageBitmap(productImage);
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(islem == 0 && resultCode == RESULT_OK && data != null){
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
productImageVeawer.setImageBitmap(bitmap);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
}
if(islem == 2){
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
System.out.println("Scan result : " + result.getContents().toString());
Set<String> set = pref.getStringSet(result.getContents().toString(), new HashSet<String>());
System.out.println("***************************** Set size : " + set.size());
ArrayList<String> arrayList = new ArrayList<>();
arrayList.addAll(set);
// barcodeText.setText(arrayList.get(0).toString()); // productNameText.setText(arrayList.get(1).toString());
}
super.onActivityResult(requestCode,resultCode, data);
}
答案 0 :(得分:0)
使用此
SharedPreferences sharedPref;
SharedPreferences.Editor editor ;
Set<String> set = new HashSet<>();
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_define_product);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPref.edit();
}
public void Save(View view){
editor.putStringSet(barcode.getText().toString(),set);
editor.apply();
Toast.makeText(getApplicationContext(),"Saved", Toast.LENGTH_LONG).show();
set.clear();
}
并在另一个活动中获取数据
public NameActivity extands AppCompactActivity{
SharedPreferences sharedPref;
SharedPreferences.Editor editor ;
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_define_product);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Set<String> fetch = sharePref.getStringSet(barcode.getText().toString(), null);
}
}
答案 1 :(得分:0)
您要将空的Set设置为首选项,您应该向Set中添加一些值并保存,如下所示
set.add("Test");
editor.putStringSet(barcode.getText().toString(),set);
editor.apply(); // must apply the change
答案 2 :(得分:0)
不要使用PreferenceManager,请使用
SharedPreferences prefs = getApplicationContext.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
为了使文件名易于记忆,我使用了程序包名称。另外,请确保在文件名周围使用“”。