Sunday, March 1, 2015

105 MySuperHero Android ArrayList and SharedPreferences


.
105 MySuperHero Android ArrayList and SharedPreferences
In the previous tutorial, we use String Array as the data source for the ListView.
String Array is static ie the initial size cannot be changed.
In this tutorial, we are going to rewrite the codes without String Array and our codes depend entirely on the ArrayList data type. It is a dynamic data structure therefore it can be used when there is no upper bound on the number of elements. It is much easier to add, replace or remove items during runtime.
We are going implement data storage using SharedPreferences so that changes are permanent and users get the last saved list instead of the default list. In this regard, ArrayList is a perfect choice since we do not know how many data items are stored in SharedPreferences when the apps is started.

0) Starting Up

1) Edit SuperHeroesActivity.java

Omit the use of String Array codes.
Use ArrayList method to add, edit or replace item in list.
Create loadData() method to get data from SharedPreferences
Create saveData() method to save data to SharedPreferences
(Most codes have been explained by the comments preceding them)
File: SuperHeroesActivity.java
package com.example.mysuperhero;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class SuperHeroesActivity extends Activity {
 Button btnAddSuperHero;
 ListView lvwSuperHeroes;
 List < String > lstSuperHeroes = new ArrayList < String > (); //init lstSuperHeroes
 ArrayAdapter adpSuperHeroes;
/* String Array declaration has been omitted */
 static final int ADD_ITEM = 1; // The request code to add item
 static final int EDIT_ITEM = 2; // The request code to edit item
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_super_heroes);
  setLvwSuperHeroes();
  setBtnAddSuperHero();
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_super_heroes, menu);
  return true;
 }
 private void setLvwSuperHeroes() {
  //bind object
  lvwSuperHeroes = (ListView) findViewById(R.id.lvw_superheroes);
  /* String Array codes have been omitted */
  //load data from SharedPreferences to lstSuperHeroes if exists
  loadData();
  //define adpSuperHeroes having data source from lstSuperHeroes
  adpSuperHeroes = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, lstSuperHeroes);
  //Plug adpSuperHeroes to lvwSuperHeroes
  lvwSuperHeroes.setAdapter(adpSuperHeroes);
  //set onclick listener for listview
  lvwSuperHeroes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
    Intent intent = new Intent(SuperHeroesActivity.this, SuperHeroActivity.class);
    intent.putExtra("POSITION", position);
    intent.putExtra("NAME", lvwSuperHeroes.getItemAtPosition(position).toString());
    startActivityForResult(intent, EDIT_ITEM);
   }
  });
  //set onItemLongClickListener
  lvwSuperHeroes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
   public boolean onItemLongClick(AdapterView < ? > arg0, View v,
    int index, long arg3) {
    deleteSuperHero(index);
    //save changes (DELETE_ITEM) to SharedPreferences    
    saveData();
    return true;
   }
  });
 }
 private void setBtnAddSuperHero() {
  btnAddSuperHero = (Button) findViewById(R.id.btn_add_superhero);
  btnAddSuperHero.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
    Intent intent = new Intent(SuperHeroesActivity.this, SuperHeroActivity.class);
    startActivityForResult(intent, ADD_ITEM);
   }
  });
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  String strNewName = "";
  int intPosition = -1;
  if (resultCode == RESULT_OK) {
   if (requestCode == ADD_ITEM) {
    try {
     strNewName = data.getStringExtra("NAME");
    } catch (Exception e) {}
    addSuperHero(strNewName);
   }
   if (requestCode == EDIT_ITEM) {
    try {
     intPosition = data.getIntExtra("POSITION", -1);
     strNewName = data.getStringExtra("NAME");
    } catch (Exception e) {}
    editSuperHero(intPosition, strNewName);
   }
   //save changes (ADD_ITEM or EDIT_ITEM) to SharedPreferences
   saveData();
  } else if (resultCode == RESULT_CANCELED) {
   //Toast.makeText(getApplicationContext(), "No Changes", //Toast.LENGTH_SHORT).show();
  }
 }
 private void editSuperHero(int position, String text) {
  //check that we have a valid position value
  if (position < 0) {
   return;
  }
  /* String Array codes have been omitted */
  //replace item in lstSuperHeroes
  lstSuperHeroes.set(position, text);
  //notify apps that adapter has changed
  adpSuperHeroes.notifyDataSetChanged();
  //scroll to the bottom
  lvwSuperHeroes.smoothScrollToPosition(adpSuperHeroes.getCount() - 1);
 }
 private void addSuperHero(String strNewName) {
  /* String Array codes have been omitted */
  //add new item to lstSuperHeroes
  lstSuperHeroes.add(strNewName);
  //notify apps that adapter has changed
  adpSuperHeroes.notifyDataSetChanged();
  //scroll to the bottom
  lvwSuperHeroes.smoothScrollToPosition(adpSuperHeroes.getCount() - 1);
 }
 private void deleteSuperHero(int position) {
  //delete item in lstSuperHeroes
  lstSuperHeroes.remove(position);
  //notify apps that adapter has changed        
  adpSuperHeroes.notifyDataSetChanged();
 }
 private void loadData() {
  //get data from SharedPreferences Resource identified by appsdata
  SharedPreferences spAppsData = getSharedPreferences("appsdata", MODE_PRIVATE);
  //get comma-separated values from data item identified by superheroes (key)
  String csvSuperHeroes = spAppsData.getString("superheroes", "");
  if (!csvSuperHeroes.equals("")) {
   //convert comma-separated value to string array using split method
   //create new list array having values from string array
   lstSuperHeroes = new ArrayList < String > (Arrays.asList(csvSuperHeroes.split(",")));
  }
 }
 private void saveData() {
  //set data SharedPreferences Resource identified by appsdata
  SharedPreferences spAppsData = getSharedPreferences("appsdata", MODE_PRIVATE);
  //set edit mode
  SharedPreferences.Editor e = spAppsData.edit();
  //prepare comma-separated values from lstSuperHeroes
  String csvSuperHeroes = TextUtils.join(",", lstSuperHeroes);
  //put values into data item identified by superheroes (key)
  e.putString("superheroes", csvSuperHeroes);
  //make permanent
  e.commit();
 }
}
Start the apps.
You should get an empty list
Add Thor, Hulk and X-men.
Delete Hulk.
Close the app.
Start the apps again and check that the correct data items persist.

DOWNLOAD

REFERENCES


.

No comments:

Post a Comment