An analysis of the realization of Android music idioms

Source: Internet
Author: User

an analysis of the realization of Android music idioms
Below is


The catalogue works as follows:

specific implementations and issues encountered in the process of writingThe first step: Build a database, such as a large number of data, you can use the EXECL table to do, and then navict visualization tools, import into load the data database into the project, create a raw folder under the Res directory, Dbopenhelper.java
Package Cn.deu.bztc.happyidiom.db;import Java.io.file;import Java.io.fileoutputstream;import java.io.InputStream; Import Cn.deu.bztc.happyidiom.activity.r;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.os.environment;public class Dbopenhelper {private final int buffwer_size=400000;//buffer size public static final string db_name= "idioms.db";//Saved database file name public static final string Package_name= "cn.deu.bztc.happyidiom.activity";//Application Package name public static final String db_path= "/data" + Environment.getdatadirectory (). GetAbsolutePath () + "/" +package_name+ "/databases";//location of the database in the phone private Context Context;public Dbopenhelper (Context context) {super (); this.context = context;} Public Sqlitedatabase OpenDatabase () {try{file mydatapath=new File (Db_path); if (!mydatapath.exists ()) { Mydatapath.mkdirs (); If you do not have this directory then create}string dbfile=mydatapath+ "/" +db_name;if (! New file (DBFile). Exists ())) {//Determine if the database file exists or import if it does not exist, or open the database InputStream is=context.getresources () directly. Openrawresource (R.raw.Idioms); FileOutputStream fos=new FileOutputStream (dbfile); byte[] Buffer=new byte[buffwer_size];int count=0;while ((count= Is.read (buffer)) >0) {fos.write (buffer, 0, count);} Fos.close (); Is.close ();} Sqlitedatabase db=sqlitedatabase.openorcreatedatabase (dbfile, null); return DB;} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} return null;}}
The above code implementation function is mainly to use the input and output stream to copy the idioms.db to the phone where the default storage database Let's test the success. setting up a unit test environment in Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Cn.deu.bztc.happyidiom.activity "android:versioncode=" 1 "android:versionname=" 1.0 "> < USES-SDK android:minsdkversion= "android:targetsdkversion="/> <application android:al Lowbackup= "true" android:icon= "@drawable/logo" android:label= "@string/app_name" Android:screenorienta tion= "Portrait" Android:theme= "@android: Style/theme.translucent.notitlebar" > <span style= "color: #ff000 0; " > <uses-library android:name= "android.test.runner"/></span> <activity android:name= "c N.deu.bztc.happyidiom.activity.mainactivity "android:label=" @string/app_name "> <intent-filter > <action android:name= "Android.intent.action.MAIN"/> <category android:name= "a Ndroid.intent.category.LAUNCHER "/&GT </intent-filter> </activity> <activity android:name= "Cn.deu.bztc.happyidiom.activi Ty.            Studyanimalactivity "android:label=" @string/app_name "> </activity> <activity Android:name= "Cn.deu.bztc.happyidiom.util.DialogUtil" android:label= "@string/app_name" > </act ivity> <activity android:name= "Cn.deu.bztc.happyidiom.activity.StudyActivity" android:l Abel= "@string/app_name" > </activity> <activity android:name= "Com.tangsci.android.exa Mple.ttsdemo.TtsDemoActivity "android:label=" @string/app_name "> </activity> </applicatio n> <span style= "color: #ff0000;" ><instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= "Cn.deu. Bztc.happyidiom.activity "> </instrumentation></span></manifest>

Let's test it for success: build Dbopenhelptest.java under the test package
Package Cn.deu.bztc.happyidiom.test;import Java.util.list;import Cn.deu.bztc.happyidiom.dao.animaldao;import Cn.deu.bztc.happyidiom.db.dbopenhelper;import Cn.deu.bztc.happyidiom.entity.animal;import Android.test.androidtestcase;public class Dbopenhelptest extends Androidtestcase {public void testdbcopy () { Dbopenhelper  dbopenhelper=new Dbopenhelper (GetContext ());d bopenhelper.opendatabase (); public void Testgetallanimals () {Animaldao animaldao=animaldao.getinstance (GetContext ()); List<animal> animals=animaldao.getallanimals (); System.out.println (Animals.size ()); for (Animal animal:animals) {System.out.println (Animal.getname ());}}}

Then you will find multiple databases in the raw directory
Cn.deu.bztc.happyidiom.entity creating entity Classes
Animal.java
Package Cn.deu.bztc.happyidiom.entity;public class Animal {private int id; private string name;//idiom name private string pronounce;//idiom pronounce private string explain;//idiom explanation private string antonym;// antonym private string homoionym;//synonym private string derivation;//derived from private string examples;//example public int getId () {return ID ;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getpronounce () {return pronounce;} public void Setpronounce (String pronounce) {this.pronounce = pronounce;} Public String Getexplain () {return explain;} public void Setexplain (String explain) {this.explain = explain;} Public String getantonym () {return antonym;} public void setantonym (String antonym) {this.antonym = antonym;} Public String gethomoionym () {return homoionym;} public void sethomoionym (String homoionym) {this.homoionym = homoionym;} Public String getderivation () {return derivation;} public void Setderivation (String derivation) {This.derIvation = derivation;} Public String Getexamples () {return examples;} public void Setexamples (String examples) {this.examples = examples;}}
Then the operational class is established in the database management layer (DAO layer) Animaldao.java
Package Cn.deu.bztc.happyidiom.dao;import Java.util.arraylist;import Java.util.list;import Cn.deu.bztc.happyidiom.db.dbopenhelper;import Cn.deu.bztc.happyidiom.entity.animal;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;public Class  Animaldao {private static Animaldao animaldao;private static sqlitedatabase db;/** * The construction method is privatized */public Animaldao (Context Context) {Dbopenhelper dbhelper=new dbopenhelper (context);d b=dbhelper.opendatabase ();} /** * Get Animaldao instance */public synchronized static Animaldao getinstance (context context) {if (animaldao==null) {animaldao= New Animaldao (context);} return Animaldao;} /** * Read all animal idioms from the database */public static list<animal> getallanimals () {list<animal> list=new arraylist< Animal> (); Cursor cursor=db.query ("Animal", NULL, NULL, NULL, null,null,null), if (Cursor.movetofirst ()) {Do{animal animal=new Animal (); Animal.setid (Cursor.getint (Cursor.getcolumnindex ("_id")); Animal.setname (Cursor.getstriNg (Cursor.getcolumnindex ("name"))); Animal.setpronounce (Cursor.getstring (Cursor.getcolumnindex ("pronounce")); Animal.setantonym (Cursor.getstring (Cursor.getcolumnindex ("antonym")); Animal.sethomoionym (Cursor.getString ( Cursor.getcolumnindex ("homoionym")); Animal.setderivation (Cursor.getstring (Cursor.getcolumnindex ("derivation") ); Animal.setexamples (Cursor.getstring (Cursor.getcolumnindex ("examples")); List.add (animal);} while (Cursor.movetonext ());} return list;}}
Unit tests are below under the test class Join
public void Testgetallanimals () {Animaldao animaldao=animaldao.getinstance (GetContext ()); List<animal> animals=animaldao.getallanimals (); System.out.println (Animals.size ()); for (Animal animal:animals) {System.out.println (Animal.getname ());}
Test as follows
After the test is successful, the implementation function
UI Interface
Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =".        Mainactivity "android:background=" @drawable/bg_animal "> <tabhost android:id=" @android: Id/tabhost " Android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:layout_alignparentleft= " True "android:layout_alignparenttop=" true "> <linearlayout android:layout_width=" match_pare NT "android:layout_height=" Match_parent "android:orientation=" vertical "> <framelay Out android:id= "@android: Id/tabcontent" android:layout_width= "Match_parent" a                    ndroid:layout_height= "Match_parent" android:layout_weight= "1" > <linearlayout         Android:id= "@+id/tab2"           Android:layout_width= "Match_parent" android:layout_height= "Match_parent"                    android:orientation= "vertical" > </LinearLayout> <linearlayout Android:id= "@+id/tab3" android:layout_width= "Match_parent" android:layout_height= " Match_parent "android:orientation=" vertical "> </LinearLayout> </ framelayout> <tabwidget android:id= "@android: Id/tabs" android:layout_width= "Match_parent" android:layout_height= "wrap_content" > </TabWidget> </linearla Yout> </TabHost></RelativeLayout>

I. Tabhost INTRODUCTION
The Tabhost component can store multiple tabs in the interface, and many of the software uses a modified component for design;
1. Tabhost Common Components
Tabwidget: This component is the Tabhost tab in the upper or lower buttons, you can click the button Switch tab;
Tabspec: Represents the Tab interface, add a tabspec can be added to the tabhost;
--Create Tab: Newtabspec (String tag), create a tab;
--Add Tab: AddTab (Tabspec);

Please see http://blog.csdn.net/harvic880925/article/details/17120325/for the use of tabhost.
It is then defined in the String.xml file in the values directory of the Res
<string-array name= "category" >        <item> Animal </item>        <item> Nature category </item>        < item> people </item>        <item> seasons </item>        <item> numbers </item>        <item> Fables </item>        <item> Other categories </item>    </string-array>
Next, build Mainactivity.java in Cn.deu.bztc.happyidiom.activity.
Package Cn.deu.bztc.happyidiom.activity;import Android.os.bundle;import Android.app.activity;import Android.app.tabactivity;import Android.content.intent;import Android.view.menu;import Android.view.Window;import Android.widget.tabhost;public class Mainactivity extends Tabactivity {private tabhost tabhost; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_ title);//Setcontentview (R.layout.activity_main); Tabhost=gettabhost (); AddTab ("study", R.string.title_study, R.drawable.study,studyactivity.class); AddTab ("Search", R.string.title_search, R.drawable.search, Studyactivity.class); AddTab ("study", R.string.title_game, R.drawable.game, Studyactivity.class); AddTab ("Save", R.string.title_save, R.drawable.save, Studyactivity.class) addTab ("Help", R.string.title_help, R.drawable.search, Studyactivity.class);} private void AddTab (String tag,int title_introduction,int title_icon,class activityclass) {Tabhost.addTab (Tabhost.newtabspec (tag) setindicator (GetString (title_introduction), Getresources (). getdrawable (Title_icon)) . SetContent (New Intent (This,activityclass)));} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Call the Tabhost component in this class, and then invoke the extracted custom method Addtob () added five tabs, the method's four parameters are the tag of each tab, the caption displayed on the indicator. , the indicator shows the contents of the picture that the tab corresponds to. Notice that the caption is canceled before the Setcontciew () method must be
below I will not introduce each one, I will share the source address to everyone.
Let's sum up the mistakes in the middle . 1. If you use the ListView control on a non-black background, Android may default to the background of this list control suddenly black when scrolling the ListView. This may lead to the black background of the program and the theme of the main program is neither coordinated
I started with the androidmanifest.xml. Android:theme= "@android: Style/theme.notitlebar"
After careful investigation of the meaning is: background theme without the title bar style, default if not set, display black background
to solve this problem: then replace it with android:theme= "@android: Style/theme.translucent.notitlebarFor more information, see http://blog.csdn.net/zhupengqq/article/details/514726822.

The following error occurred: This is not an instance class

Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

There are two scenarios in which activity cannot be instantiated:

1. The activity has not been registered in the manifest.xml manifest, or the class name of the package name or activity has been modified after the activity has been created, and the configuration manifest has not been modified, resulting in the inability to instantiate

2. Create a new package yourself, while configuring, use the default package

For more information, see: http://blog.csdn.net/zhupengqq/article/details/51591964


Below is the project source code: Http://pan.baidu.com/s/1skK7cVN Password Q6ow


An analysis of the realization of Android music idioms

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.