Android 資料配接器

來源:互聯網
上載者:User

標籤:

把複雜的資料(數組、鏈表、資料庫、集合等)填充到指定的視圖介面上。 arrayAdapter(數組適配器):     用於綁定一些格式單一的資料,資料來源:資料或者集合。  private ListView listView ;        private ArrayAdapter<String> adapter ;        private SimpleAdapter simpleAdapter;        @Override        protected void onCreate(Bundle savedInstanceState) {               super.onCreate(savedInstanceState);              setContentView(R.layout. activity_main);               listView=(ListView) findViewById(R.id. listView1);               /**               * 建立一個資料配接器               * arrayadapter(上下文,當前 listview載入的每個清單項目所對應的布局檔案,所對應的資料來源)               */              String[] data={ "az","az1" };               adapter= new ArrayAdapter<String>( this ,android.R.layout. simple_list_item_1,data);               //使用視圖(listview)載入適配器               listView.setAdapter( adapter);       } SimpleAdapter(簡單適配器):     用於綁定一些格式複雜的適配器,資料來源:只能是特定的泛型集合。 private ListView listView ; private SimpleAdapter simpleAdapter ;                /**               * context:上下文               * data:資料來源(特定的泛型集合) List<? extends Map<String, ?>> data 一個MAP所組成的LIST集合               * ,每一個MAP都會對應 listview列表中的一行,每一個map中的KAY必須要包含所有在from中所指定的鍵               * from:map中的鍵名               * resource:清單項目的布局檔案               * to:綁定資料檢視中的ID,與from成對應關係               */               datalist= new ArrayList<Map<String, Object>>();               simpleAdapter= new SimpleAdapter( this, getdata(), R.layout. activity_main , new String[]{"image", "text"}, new int[]{R.id. imageView1 ,R.id. textView1});               listView.setAdapter( simpleAdapter);  private List<Map<String, Object>> getdata(){               for ( int i = 0; i < 20; i++) {                     Map<String, Object>map= new HashMap<String,Object>();                     map.put( "image", R.drawable. ic_launcher);                     map.put( "text", "az" );                      datalist.add(map);              }               return datalist ;       } 資料配接器是連結資料來源和視圖介面的橋樑。 實現過程:建立適配器——添加資料來源到適配器——視圖載入適配器

Android 資料配接器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.