I have a problem reading getSharedPreferences from the non-Activity class to set a playlist in the player ... In my work, I take a string variable from edittext to get the path to the folder for working with audio files ...
public class MainActivity extends Activity {
String ppp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
if (PATH == null){
..........
...........
path_tv.setText("folder is undefined");
}
else {
path_tv.setText("folder defined: /mnt/sdcard/" + PATH);
}
set_path.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (path_edit.getText().toString().length() == 0){
Toast.makeText(getBaseContext(), "folder is undefined", Toast.LENGTH_SHORT).show();
}
else {
ppp = path_edit.getText().toString();
getSharedPreferences("PATH", MODE_PRIVATE)
.edit()
.putString("path", ppp)
.commit();
File folder = new File(Environment.getExternalStorageDirectory() + "/" + ppp);
boolean success = false;
if (!folder.exists()) {
success = folder.mkdir();
if (success) Toast.makeText(getBaseContext(), ".....", Toast.LENGTH_SHORT).show();
}
String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
path_tv.setText("........ /mnt/sdcard/" + PATH);
path_edit.setText("");
}
}
});
So, in action, I can change and save the String value in the general settings ... But ho, can I do this from an open class ...? Please rate any examples ...
source
share