Android開(kāi)發(fā)中如何進(jìn)行數(shù)據(jù)綁定DataBinding

Android開(kāi)發(fā)中如何進(jìn)行數(shù)據(jù)綁定Data Binding,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比大姚網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式大姚網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋大姚地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

前面提到AndroidGraphics2DTutorial說(shuō)過(guò)它是ListActivity派生出來(lái)的。ListActivity中顯示的是 ListView,ListView和Gallery  ,Spinner有一個(gè)共同點(diǎn):它們都是AdapterView的子類。AdapterView的顯示可以通過(guò)數(shù)據(jù)綁定來(lái)實(shí)現(xiàn),數(shù)據(jù)源可以是數(shù)組或是數(shù)據(jù)庫(kù)記錄,數(shù)據(jù)源和AdapterView是通過(guò)Adapter作為橋梁。通過(guò)Adapter,AdatperView可以顯示數(shù)據(jù)源或處理用戶選取時(shí)間, 如:選擇列表中某項(xiàng)。

Android開(kāi)發(fā)中如何進(jìn)行數(shù)據(jù)綁定Data Binding

AndroidGraphics2DTutorial讀取AndroidManifest.xml中Intent-Filter為

<action android:name=”android.intent.action.MAIN” />
<category android:name=”com.pstreets.graphics2d.SAMPLE_CODE” />

的所有Activity,以列表方式顯示。使用了Android API 自帶的SimpleAdapter。 來(lái)看看AndroidGraphics2DTutorial.java 中相關(guān)代碼:

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(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[] labellabelPath = 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);      }     }

使用數(shù)據(jù)顯示Layout,上面代碼中

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

為L(zhǎng)istActivity中ListView  指定Adapter,這個(gè)Adapter的數(shù)據(jù)源為getData(),getData()從Manifest.xml中查找出所有符合條件的示例 Activity列表。  這里DataSource是靜態(tài)的從文件中讀取,如果DataSource為數(shù)組或是其它數(shù)據(jù)源,如果程序中修改數(shù)值的內(nèi)容,則你應(yīng)該 notifyDataSetChanged()來(lái)通知UI數(shù)據(jù)有變動(dòng)。UI則會(huì)刷新顯示以反映數(shù)據(jù)變化。簡(jiǎn)單的說(shuō)Android數(shù)據(jù)綁定和.Net  WinForm ,WPF 中數(shù)據(jù)綁定類似。

處理用戶選取事件,AdapterView.OnItemClickListener()可以用來(lái)處理選取事 件,對(duì)于ListActivity,可以用protected void onListItemClick(ListView l, View v,  int position, long  id)。AndroidGraphics2DTutorial中的實(shí)現(xiàn)是用戶選取Activity名稱好,則啟動(dòng)對(duì)應(yīng)的Activity。

上面代碼中使用SimpleAdapter,并使用Android提供的android.R.layout.simple_list_item_1 來(lái)顯示數(shù)據(jù),Andrid也允許使用自定義的Layout來(lái)顯示數(shù)據(jù)。

關(guān)于Android開(kāi)發(fā)中如何進(jìn)行數(shù)據(jù)綁定Data Binding問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

新聞標(biāo)題:Android開(kāi)發(fā)中如何進(jìn)行數(shù)據(jù)綁定DataBinding
URL分享:http://bm7419.com/article42/igoeec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)做網(wǎng)站、App開(kāi)發(fā)、微信小程序網(wǎng)站改版、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)