Guest User

Untitled

a guest
Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. public class frontPage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  2.  
  3. public static ArrayList<String> posts = new ArrayList<>();
  4. ArrayAdapter<String> adapter;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_front_page);
  10. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  11. setSupportActionBar(toolbar);
  12. loadSavedPreferences();
  13. ListView entryList = (ListView) findViewById(R.id.entryList);
  14.  
  15. if (posts != null && posts.size() > 0) {
  16. adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, posts.toArray(new String[1]));
  17. entryList.setAdapter(adapter);
  18. }
  19.  
  20. entryList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  21. @Override
  22. public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  23. posts.remove(position);
  24. adapter.notifyDataSetChanged();
  25.  
  26. Toast.makeText(frontPage.this, "Item Deleted", Toast.LENGTH_LONG).show();
  27. return true;
  28. }
  29. });
  30.  
  31.  
  32. private void savePreferences() {
  33. SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
  34. String entryString = "";
  35.  
  36. for(int i = 0; i < posts.size(); i++) {
  37. entryString += posts.get(i) + "|";
  38. }
  39. SharedPreferences.Editor editor = sharedPreferences.edit();
  40. editor.putString("posts", entryString);
  41. editor.commit();
  42.  
  43. }
  44.  
  45. private void loadSavedPreferences() {
  46. SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
  47. String entryString = sharedPreferences.getString("posts", "");
  48. StringTokenizer tokenizer = new StringTokenizer(entryString, "|");
  49. while(tokenizer.hasMoreTokens()) {
  50. posts.add(tokenizer.nextToken());
  51. SharedPreferences.Editor editor = sharedPreferences.edit();
  52. editor.putString("posts", entryString);
  53. editor.commit();
  54. }
  55. }
  56.  
  57. @Override
  58. protected void onDestroy() {
  59. super.onDestroy();
  60. savePreferences();
  61. finish();
  62. }
Add Comment
Please, Sign In to add comment