Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class frontPage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
- public static ArrayList<String> posts = new ArrayList<>();
- ArrayAdapter<String> adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_front_page);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- loadSavedPreferences();
- ListView entryList = (ListView) findViewById(R.id.entryList);
- if (posts != null && posts.size() > 0) {
- adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, posts.toArray(new String[1]));
- entryList.setAdapter(adapter);
- }
- entryList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
- posts.remove(position);
- adapter.notifyDataSetChanged();
- Toast.makeText(frontPage.this, "Item Deleted", Toast.LENGTH_LONG).show();
- return true;
- }
- });
- private void savePreferences() {
- SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
- String entryString = "";
- for(int i = 0; i < posts.size(); i++) {
- entryString += posts.get(i) + "|";
- }
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString("posts", entryString);
- editor.commit();
- }
- private void loadSavedPreferences() {
- SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
- String entryString = sharedPreferences.getString("posts", "");
- StringTokenizer tokenizer = new StringTokenizer(entryString, "|");
- while(tokenizer.hasMoreTokens()) {
- posts.add(tokenizer.nextToken());
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString("posts", entryString);
- editor.commit();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- savePreferences();
- finish();
- }
Add Comment
Please, Sign In to add comment