-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsharedPreference.java
61 lines (48 loc) · 1.98 KB
/
sharedPreference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class sharedPreference {
}
/////////////////// Shared Preference
Spinner movies;
EditText ticket;
Button submit;
String[] moviesName = {"Animal","Avatar","RRR","KGF"};
int selectedPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movies = findViewById(R.id.spinner);
ticket = findViewById(R.id.ticket);
submit = findViewById(R.id.submitBtn);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,moviesName);
movies.setAdapter(adapter);
movies.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
selectedPosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences = getSharedPreferences("MyPreferance",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("MovieName",selectedPosition);
editor.putInt("TicketsCount",Integer.parseInt(ticket.getText().toString()));
editor.apply();
Toast.makeText(MainActivity.this, "Saved Successfully", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sh = getSharedPreferences("MyPreferance",MODE_PRIVATE);
int spos = sh.getInt("MovieName",0);
int ct = sh.getInt("TicketsCount",0);
movies.setSelection(spos);
ticket.setText(String.valueOf(ct));
}
}