I. Summary of technologies involved in development:
Tabhost tab component, pagerAdapter Implementation of Sliding Screen between left and right, json data parsing, xml data parsing, ListActivity and BaseAdapter combined to display the list of data and images, handler updates the UI, the Singleton mode enables data sharing throughout the project, httpClient access to the network, and sqlite lightweight databases.
2. program running effect display:
Home Page
3. key code Annotations:
1. Use the tabhost component:
The layout file code is as follows:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/yun4" android:orientation="vertical" ><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/biaotouFramelayout" android:layout_width="fill_parent" android:layout_height="64dp" > <!-- <ImageButton android:id="@+id/shuaxinButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="10dp" android:src="@drawable/shuaxin" /> --> <ImageView android:id="@+id/imageView5" android:layout_width="53dp" android:layout_height="65dp" android:src="@drawable/weather" /> <TextView android:id="@+id/title_textview" style="@style/activity_title_text" android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <ImageButton android:id="@+id/shuaxinImageBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/shuaxin" /></RelativeLayout> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="1dip" android:visibility="gone" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="53dp" android:paddingTop="5dip" > <LinearLayout android:id="@+id/channel1" style="@style/main_tab_but_linear" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="54dp" android:src="@drawable/tianqi" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> </LinearLayout> <LinearLayout android:id="@+id/channel2" style="@style/main_tab_but_linear" > <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="52dp" android:src="@drawable/qushi" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> </LinearLayout> <LinearLayout android:id="@+id/channel3" style="@style/main_tab_but_linear" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="52dp" android:src="@drawable/shezhi" /> </LinearLayout> <LinearLayout android:id="@+id/channel4" style="@style/main_tab_but_linear" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <ImageView android:id="@+id/imageView4" android:layout_width="fill_parent" android:layout_height="54dp" android:src="@drawable/gengduo" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> </LinearLayout> </LinearLayout> </LinearLayout></TabHost>
A. the activity that uses tabhost must inherit TabActivity and call getTabHost () to obtain the tabhost object.
B. Use the tabhost tab to display the tabHost interface.
. NewTabSpec (tagName)
. SetIndicator (getResources (). getString (tagLable ),
GetResources (). getDrawable (icon )). setContent (content); The setIndicator () method is used to set the background and text of the tab button. You can add a TextView object with a transparent background to make the button background transparent.
2. xml parsing, including reading, deleting, and writing data
The custom xml format is as follows:
<? Xml version = "1.0" encoding = "GB2312"?> -<City> <item> for example, </item> <item> Shenyang </item> <item> Changsha </item> <item> Wuhan </item> </city>
Public class xmlmanager {private context; documentbuilderfactory docbuilderfactory = NULL; documentbuilder docbuilder = NULL; document DOC = NULL; file = new file (environment. getexternalstoragedirectory () + file. separator + "citydata" + file. separator + "city. XML "); // path of the file to be output public xmlmanager (context) {This. context = context; docbuilderfactory = documentbuilderfactory. newinstance (); Try {docbuilder = docbuilderfactory. newdocumentbuilder (); Doc = docbuilder. parse (New bufferedinputstream (New fileinputstream (File); Doc. normalize ();} catch (parserconfigurationexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (saxexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} // Put the XML file in the Assets Directory} public void readxml () {// root elementelement root = Doc. getdocumentelement (); nodelist = root. getelementsbytagname ("item"); log. I ("nodesize", String. valueof (root. getchildnodes (). getlength (); For (INT I = 0; I <nodelist. getlength (); I ++) {element nD = (element) nodelist. item (I); log. I ("Node", nd. getfirstchild (). getnodevalue (); citydatamanager. getinstance (). add (Nd. getfirstchild (). getnodevalue ();} log. I ("Node", String. valueof (citydatamanager. getinstance (). getitems (). size ();} public void removeitem (string name) {element root = (element) (Doc. getdocumentelement (). getelementsbytagname ("city "). item (0); log. I ("Node cityname", name); For (INT I = 0; I <root. getelementsbytagname ("item "). getlength (); I ++) {Element E = (element) root. getelementsbytagname ("item "). Item (I); log. I ("Node vlaue", E. getfirstchild (). getnodevalue (); If (E. getfirstchild (). getnodevalue (). equals (name) {If (root. removechild (e )! = NULL) Save () ;}}public void additem (string name) {element root = (element) (Doc. getdocumentelement (). getelementsbytagname ("city "). item (0); log. I ("root", String. valueof (root. getelementsbytagname ("item "). getlength (); element citynode = Doc. createelement ("item"); citynode. appendchild (Doc. createtextnode (name); // citynode. setnodevalue ("Beijing"); root. appendchild (citynode); log. I ("root", String. valueof (root. ge Telementsbytagname ("item"). getlength (); Save ();} private void save () {If (! Environment. getexternalstoragestate (). equals (environment. media_mounted) {// return does not exist in sdcard; // return code called} file = new file (environment. getexternalstoragedirectory () + file. separator + "citydata" + file. separator + "city. XML "); // path of the file to be output if (! File. getparentfile (). exists () {// The parent path does not exist file. getparentfile (). mkdirs (); // create parent folder} transformerfactory tfactory = transformerfactory. newinstance (); transformer = NULL; try {transformer = tfactory. newtransformer ();} catch (transformerconfigurationexception e) {// todo auto-generated catch blocke. printstacktrace ();} transformer. setoutputproperty (outputkeys. encoding, "gb2312"); domsource source = new domsource (DOC); streamresult result = new streamresult (File); try {transformer. transform (source, result);} catch (transformerexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}
3. Application of SQLite database on Android platform:
Public class DBManager {public static final String _ DB_PATH = "/data/com. android. weather/databases "; // private final int BUFFER_SIZE = 400000; public static final String DB_NAME =" db_weather.db "; // The saved database file name is public static final String PACKAGE_NAME =" com. android. weather "; public static final String DB_PATH ="/data "+ Environment. getDataDirectory (). getAbsolutePath () + "/" + PACKAGE_NAME; // save it on your phone Database location (/data/com.css ystem. activity/cssystem. db) private SQLiteDatabase database; private Context context; public DBManager (Context context) {this. context = context;} public SQLiteDatabase getDatabase () {return database;} public void setDatabase (SQLiteDatabase database) {this. database = database;} public void openDatabase () {System. out. println (DB_PATH + "/" + DB_NAME); this. database = this. openDatab Ase (DB_PATH + "/" + DB_NAME);} private SQLiteDatabase openDatabase (String dbfile) {try {if (! (New File (dbfile ). exists () {// determines whether the database file exists. If it does not exist, execute import. Otherwise, directly open the database InputStream is = this. context. getResources (). openRawResource (R. raw. db_weather); // The database FileOutputStream fos = new FileOutputStream (dbfile); byte [] buffer = new byte [is. available ()]; int count = 0; while (count = is. read (buffer)> 0) {fos. write (buffer, 0, count);} fos. close (); is. close ();} SQLiteDatabase db = SQLiteDatabase. openOrCreateD Atabase (dbfile, null); return db;} catch (FileNotFoundException e) {Log. e ("Database", "File not found"); e. printStackTrace ();} catch (IOException e) {Log. e ("Database", "IO exception"); e. printStackTrace ();} return null;} public void closeDatabase () {this. database. close ();} public String getCityCodeByName (String cityName) {Cursor cursor = database. query ("cities", new String [] {"city_num"}, "name =? ", New String [] {cityName}, null); String cityCode = null; if (! Cursor. isLast () {cursor. moveToNext (); cityCode = cursor. getString (0);} cursor. close (); // database. close (); return cityCode ;}}
4. Use pagerview to implement sliding screen between left and right
Layout File Code
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:theme="@android:style/Theme.Translucent" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:id="@+id/viewGroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> </RelativeLayout> </FrameLayout>
Activity call:
main = (ViewGroup) inflater.inflate(R.layout.circle_viewflow_layout,null);group = (ViewGroup) main.findViewById(R.id.viewGroup);viewPager = (ViewPager) main.findViewById(R.id.viewPager);
Add the view to be loaded through the list <View> array, and then add the array to the myadatper that inherits the pageradapter.
MyAdapter myadapter = new MyAdapter(list, WeatherMainActivity.this,inflater, viewPager);MySingleInstance.getInstance().setMyadapter(myadapter);viewPager.setAdapter(myadapter);createDot();MyDotHandler dotHandler = new MyDotHandler();HandlerApp.getInstance().setDotHandler(dotHandler);
Note: It is best to customize a list array in the adatper adapter. After updating the data, call the following method to update the UI.
MySingleInstance.getInstance().getMyadapter().notifyDataSetChanged();
5. Use handler to update the UI
Custom Handler
Public class myhandler extends handler {@ overridepublic void handlemessage (Message MSG) {super. handlemessage (MSG); If (MSG. what = changed) {// update uimlistadapter = new mylistadapter (moreactivity. this, citylistitemsdatamanager. getinstance (). getlistdata (); setlistadapter (mlistadapter );}}}
Instantiate handler in Activity
protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);instance = this;Log.i("listadapter",String.valueOf(CityListItemsDataManager.getInstance().getListData().size()));mListAdapter = new MyListAdapter(this, CityListItemsDataManager.getInstance().getListData());setListAdapter(mListAdapter);mHandler = new MyHandler();HandlerApp.getInstance().setMyHandler(mHandler);}
After updating the data, you must obtain the instantiated handler object and send the change message before calling the method in handler to implement UI update.
HandlerApp.getInstance().getDotHandler().sendEmptyMessage(CHANGED);
Source code: Click to open the link