Android development-data storage I, android Data Storage

Source: Internet
Author: User

Android development-data storage I, android Data Storage

This chapter describes simple data storage in Android development. Knowledge is mainly involved in SharedPreferences and multi-page switching of ViewPager.

1. Functional Requirements

Make a small application, there is a left and right guide chart at startup, and it is only displayed at the first startup. After reading it, it will not be displayed at the next startup.

  • Present and guide two activities and enter the main interface
  • Click to jump to the next page, the main interface is not limited to content
  • The boot page is no longer displayed at the second startup, and you can directly jump to the application Main Interface
2. Software Implementation

Figure 1

Figure 2

Figure 3

Brief description: run the software to go to the figure 1 interface. If the software is used for the first time, it automatically jumps to the guiding interface after 2 seconds. By sliding the screen, you can jump to different boot interfaces. When you click to enter the home page, record the status value displayed on the boot page. If the null value is not clear, the next boot page will not appear.

3. Related Knowledge (1) SharedPreferences

SharedPreferencesData Persistence means to store simple types of data (such as String and int. For complex types of data, it is generally to convert the data into Base64 encoding, and then save the converted data in the XML file as a string, and then save it with SharedPreferences.

SharedPreferences usage:

  • Use the getSharedPreferences method of the Activity class to obtain the SharedPreferences object. The name of the file storing the key-value is specified by the first parameter of the getSharedPreferences method;
  • Use the edit operation of the SharedPreferences interface to obtain the SharedPreferences. Editor object;
  • Use the putXxx method of the SharedPreferences. Editor interface to save the key-value pair. Xxx indicates different data types. For example, the putString method is required for string-type values;
  • Use the commit method of the SharedPreferences. Editor interface to save the key-value pair. The commit method is equivalent to a commit operation in a database transaction.

In this example, the case code for storing whether to display the boot page is as follows:

Public class SessionData {
Public static final int VERSION = 1; // 1 does not start the cache
Public static SharedPreferences sp_version; // save
}

// Store and submit data
SessionData. sp_version.edit (). putInt ("VERSION", SessionData. VERSION). commit ();

// Read the data required by SharedPreferences
SessionData. sp_version = getSharedPreferences ("Y_Setting", Context. MODE_PRIVATE );

Int version = SessionData. sp_version.getInt ("VERSION", 0 );

(2) ViewPager

Android-support-v4.jar is a software package officially provided by Google to US compatible with low version Android devices, which contains only in Android 3.0 and above can be used api. Viewpager is one of them. We can do many things, from the simplest navigation to the page menu. So how can we use it? Like ListView, we also need an adapter, Which is PagerAdapter. The ViewPager class provides new effects for multi-interface switching. The new effects have the following features:

  • One of the interfaces is displayed;
  • When a user slides between the left and right interfaces, the current screen displays the current interface and a part of the next interface;
  • After the slide ends, the page automatically jumps to the selected page.

The code used by ViewPager in this example is as follows:

1 package www.csnt.com. geekbandfivehomework; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. support. v4.view. viewPager; 7 import android. support. v4.view. viewPager. onPageChangeListener; 8 import android. view. layoutInflater; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. button; 12 13 import java. util. arrayList; 14 15 public class GuideActivity extends Activity implements OnPageChangeListener {16 // defines ViewPager object 17 private ViewPager viewPager; 18 // defines an ArrayList to store View19 private ArrayList <View> views; 20 // define the View object 21 private View view1, view2, view3, view4; 22 // define the start Button object 23 private Button btnStart; 24 25 @ Override26 protected void onCreate (Bundle savedInstanceState) {27 super. onCreate (savedInstanceState); 28 setContentView (R. layout. activity_guide); 29 initView (); 30 31} 32 33/** 34 * initialize 35 */36 private void initView () {37 // instantiate ViewPager38 viewPager = (ViewPager) findViewById (R. id. viewpager); 39 40 // instantiate the layout object of each interface 41 LayoutInflater mLi = LayoutInflater. from (this); 42 view1 = mLi. inflate (R. layout. guide_view1, null); 43 view2 = mLi. inflate (R. layout. guide_view2, null); 44 view3 = mLi. inflate (R. layout. guide_view3, null); 45 46 47 // instantiate the ArrayList object 48 views = new ArrayList <View> (); 49 // mount the View to be displayed by page to 50 views in the array. add (view1); 51 views. add (view2); 52 views. add (view3); 53 54 // set the listener to 55 viewPager. setOnPageChangeListener (this); 56 // set the adapter data 57 viewPager. setAdapter (new ViewPagerAdapter (views); 58 59 // instantiate start Button 60 btnStart = (Button) view3.findViewById (R. id. startBtn); 61 // set listening 62 btnStart for the start button. setOnClickListener (new OnClickListener () {63 @ Override64 public void onClick (View v) {65 // store data and submit 66 SessionData. sp_version.edit () 67. putInt ("VERSION", SessionData. VERSION ). commit (); 68 startActivity (new Intent (GuideActivity. this, MainActivity. class); 69 finish (); 70} 71 72 }); 73} 74 75/** 76 * Call 77 */78 @ Override79 public void onPageScrollStateChanged (int arg0) when the sliding status changes) {80 81} 82 83/** 84 * Call 85 */86 @ Override87 public void onPageScrolled when sliding the current page (int arg0, float arg1, int arg2) {88 89} 90 91/** 92 * when a new page is selected, call 93 */94 @ Override95 public void onPageSelected (int arg0) {96} 97 98}ViewPager

View3 = mLi. inflate (R. layout. guide_view3, null); indicates that an interface is loaded to the project. You can use other controls in this interface as follows:

// Start of Instantiation
BtnStart = (Button) view3.findViewById (R. id. startBtn );
// Set the listener for the start button
BtnStart. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Store and submit data
SessionData. sp_version.edit ()
. PutInt ("VERSION", SessionData. VERSION). commit ();
StartActivity (new Intent (GuideActivity. this, MainActivity. class ));
Finish ();
}
});

4. Download the project source code

The source code of this project is on the 360 cloud disk and the development environment is Android Studio 2.0 beta 7.

Https://yunpan.cn/cYamkZG3sq9jf access password f3d5. File Name: android boot interface demo.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.