Lists Preferences

Friday 25 October 2013



Lists Preferences


Last time: mClickCount vs. onDestroy

See how mClickCount variant in activity object is destroyed. All the UI is preserved automatically though, and keeping the UI going is a very common case.

Second Activity / finish() / onPause()

  • Previously used intent to open web page … another app
  • Intents also used to go to our own activity
  • Super simple example of second activity (screen):
  • Layout in second.xml, code in SecondActivity.java, entry in manifest (all shown below)
  • Button calls finish() to end this activity
  • Or could hit back button
  • Or could hit home button
  • Pattern: use onPause() for “one the way out” code


    
    


----

package edu.stanford.nick;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends Activity {
 public static final String TAG = "Second";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  Log.v(TAG, "onCreate:" + savedInstanceState);
  super.onCreate(savedInstanceState);

  setContentView(R.layout.second);

  // Button calls finish() to end the activity
  // (similar to hitting the back button)
  Button b2 = (Button) findViewById(R.id.button1);
  b2.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    finish();
   }
  });
 }

 // Here we have onXXX just to log them..

 @Override
 public void onResume() {
  Log.v(TAG, "onResume");
  super.onResume();
 }

 @Override
 public void onPause() {
  Log.v(TAG, "onPause");
  super.onPause();
 }

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  Log.v(TAG, "onSaveInstanceState:" + outState);
 }

 @Override
 public void onDestroy() {
  Log.v(TAG, "onDestroy");
  super.onDestroy();
 }
}

----

Need to add in the manifest:

        ...
        
        
        ...

----

Code to start activity from button:

 // Start the SecondActivity
 Button b2 = (Button) findViewById(R.id.button2);
 b2.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 Intent myIntent = new Intent(LifecycleActivity.this.getApplicationContext(),
    SecondActivity.class);
    startActivityForResult(myIntent, 0);

}
  });

List View

  • Very common to want to show a list — choices, email headers, etc.
  • We’ll begin looking at this today, and complete the story next time with the SQLite layer
  • Create a layout containing a ListView with the special id @android:id/list
  • OR you can not create a layout at all, and ListActivity uses a default where the list just fills the screen (a common case)
  • Create MyListActivity subclassed off ListActivity
  • Create ArrayAdapter — MVC interface logic — pointers to both the view and the data
  • Call setListAdapter() to install the adapter we want (thus the data/view logic)
  • Try it: see that the list has the data
  • Click: onListItemClick() notification is called for list item click
  • Add: button calls .add() on adapter … goes to underlying data. The UI catches up
  • What happens if we leave this activity .. where is the data?


    
    
    
    


---------

package edu.stanford.nick;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

// Demonstrates a simple ListView showing data from an ArrayList.
public class MyListActivity extends ListActivity {
 // The ArrayAdpater interfaces between list view and the ArrayList of data.
 // Here the adapter is an instance var in the Activity ... not the correct
 // long-term strategy, since the activity gets destroyed.
 private ArrayAdapter mAdapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.mylist);
  // Comment out above to use the default list-fills-screen layout.

  List data = new ArrayList();
  data.add("The Prolific Oven");
  data.add("Fraiche Yogurt");
  data.add("Patxis Pizza");
  data.add("Jing Jing");
  data.add("St. Michael's Alley");

  // simple_list_item_1 is a built in "layout" for single row item.
  // Later we'll make our own little layouts for one row.
  mAdapter = new ArrayAdapter(
   this,
   android.R.layout.simple_list_item_1,
   data
  );

  setListAdapter(mAdapter);

  Button b1 = (Button) findViewById(R.id.button1);
  b1.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    addRow();
   }
  });
 }

// Notification of click on a list item -- shows a toast.
 protected void onListItemClick(ListView listView, View view, int position, long id){
  super.onListItemClick(listView, view, position, id);

  String string = (String) getListAdapter().getItem(position);
  Toast.makeText(this, "Behold: " + string, Toast.LENGTH_SHORT).show();
 }

 // Adds data to the list, via the adapter. Shows that the UI
 // sees data changes.
 // Problem: where is the data stored when this activity is gone?
 private void addRow() {
  String now = String.valueOf(System.currentTimeMillis());
  mAdapter.add(now);
 }
}

Preferences

  • The simplest way to store (“persist”) user preference data
  • By default the data is isolated to just be available to this application
  • The preferences framework takes care of both the data structure AND the UI .. you just describe what you want in XML, so it’s quite handy. You have seen this UI many times on your phone.
  • Next time, we’ll look at how to persist bulk user data, such as emails or images
  • See prefs.xml and activity code below



  

  

Preferences Activity code…
package edu.stanford.nick;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

// Trivial preferences activity
public class MyPreferenceActivity extends PreferenceActivity {
 public static final String TAG = "Lifecycle";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  addPreferencesFromResource(R.xml.prefs);
 }

 @Override
 public void onPause() {
  super.onPause();

  // Normally you don't need an onPause here -- just using it to show
  // how to retrieve the pref values in code.

        SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        // Here's how to grab a preference value
        boolean crashy = prefs.getBoolean("crashrandomly", true);
        String run = prefs.getString("runpref", "");

        System.err.println("crashy:" + crashy);
        System.err.println("run:" + run);
 }
}
Regards
Owntutorials | Cameron Thomas
MaximeTech | Best IT solutions Provider | Web Development |Mobile Applications

Read more at http://owntutorials.com/lists-preferences/#4gGBkJ1TuXcmumyr.99

No comments:

Post a Comment

 

Total Pageviews

Blogroll

Most Reading