Android Concise development Tutorial Ten: Data Binding Binding

Source: Internet
Author: User
Tags collator android data binding

As mentioned earlier, Androidgraphics2dtutorial said it was derived from listactivity. Listview,listview and Gallery are shown in listactivity, and spinner have one thing in common: they are all subclasses of Adapterview. The Adapterview display can be implemented through data binding, which can be either an array or a database record, and the data source and Adapterview are used as bridges through adapter. Adapter,adatperview enables you to display a data source or to process a user's selection time, such as selecting an item in a list.

Androidgraphics2dtutorial read Androidmanifest.xml in Intent-filter as

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Com.pstreets.graphics2d.SAMPLE_CODE"/>

All of the activity, shown in a list. Using the Android API with its own simpleadapter. Look at the code in Androidgraphics2dtutorial.java:

public class Androidgraphics2dtutorial extends Listactivity {
private static final String sample_category
= "Com.pstreets.graphics2d.SAMPLE_CODE";
@Override
public void OnCreate (Bundle savedinstancestate) {
Super. OnCreate (Savedinstancestate);
Setlistadapter (The new Simpleadapter (this, GetData (),
Android. R.layout.simple_list_item_1, new string[] {"title"},
new int [] {Android. R.ID.TEXT1}));
Getlistview (). Settextfilterenabled (True);
}
Protected List GetData () {
list<map> myData = new arraylist<map> ();
Intent mainintent = new Intent (intent.action_main, NULL);
Mainintent.addcategory (sample_category);
Packagemanager pm = Getpackagemanager ();
list<resolveinfo> list = pm.queryintentactivities (mainintent, 0);
if (null = = list)
return myData;
String[] Prefixpath;
Prefixpath = null;
int len = List.size ();
map<string, boolean> entries = new hashmap<string, boolean> ();
for (int i = 0; i < len; i++) {
Resolveinfo info = list.get (i);
Charsequence labelseq = Info.loadlabel (PM);
String label = Labelseq!= null? Labelseq.tostring ()
: Info.activityInfo.name;
string[] Labelpath = Label.split ("/");
String Nextlabel = Prefixpath = null? labelpath[0]
: Labelpath[prefixpath.length];
if ((Prefixpath!= null? prefixpath.length:0)
= = labelpath.length-1) {
AddItem (MyData,
Nextlabel,
Activityintent (
Info.activityInfo.applicationInfo.packageName,
Info.activityInfo.name));
} else {
if (Entries.get (nextlabel) = = null) {
AddItem (MyData, Nextlabel, Browseintent (Nextlabel));
Entries.put (Nextlabel, true);
}
}
}
Collections.sort (MyData, sdisplaynamecomparator);
return myData;
}
Private final static comparator<map> Sdisplaynamecomparator
= new Comparator<map> () {
Private final Collator collator = Collator.getinstance ();
public int Compare (map map1, map map2) {
Return Collator.compare (Map1.get ("title"), Map2.get ("title"));
}
};
Protected Intent activityintent (String pkg, string componentname) {
Intent result = new Intent ();
Result.setclassname (pkg, componentname);
return result;
}
Protected Intent browseintent (String path) {
Intent result = new Intent ();
Result.setclass (This, androidgraphics2dtutorial. Class);
return result;
}
protected void AddItem (list<map> data, String name, Intent Intent) {
map<string, Object> temp = new hashmap<string, object> ();
Temp.put ("title", name);
Temp.put ("intent", intent);
Data.add (temp);
}
@Override
protected void Onlistitemclick (ListView l, View V,
int position, long ID) {
Map map = (map) l.getitematposition (position);
Intent Intent = (Intent) map.get ("Intent");
StartActivity (Intent);
}
}

Use data to display layout, in the code above

setListAdapter(new SimpleAdapter(this, getData(),
android.R.layout.simple_list_item_1, new String[] { “title” },
new int[] { android.R.id.text1 }));

Specify adapter for ListView in Listactivity, the adapter data source is GetData (), and GetData () finds all eligible sample activity lists from Manifest.xml. Here the DataSource is statically read from the file, and if the DataSource is an array or other data source, you should notifydatasetchanged () to notify the UI that the data has changed if it modifies the content of the value in the program. The UI refreshes the display to reflect the data changes. Simply put, Android data binding and. Net WinForm are similar to data binding in WPF.

Handle the user selection event, Adapterview.onitemclicklistener () can be used to handle the selection event, and for listactivity, you can use protected void Onlistitemclick (ListView l, View V, int position, long ID). The implementation in Androidgraphics2dtutorial is when the user chooses the activity name and initiates the corresponding activity.

Using Simpleadapter in the code above and using the android.r.layout.simple_list_item_1 provided by Android to display the data, Andrid also allows you to use custom layout to display the data, and for this example, You can use picture highlighting to display the list, which is described later if you use custom adapter and custom layout to display bound data.

See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm

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.