Android SimpleAdapter,SimpleCursorAdapter,ArrayAdapter的區別

來源:互聯網
上載者:User

資料來源不同而已
1. String[]: ArrayAdapter
2. List<Map<String,?>>: SimpleAdapter
3. 資料庫Cursor: SimpleCursorAdapter

使用ArrayAdapter(數組適配器)顧名思義,需要把資料放入一個數組以便顯示。
android.R.layout.simple_list_item_1是系統定義好的布局檔案只顯示一行文字

SimpleAdapter能定義各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(複選框)

//=============================SimpleAdapter==========================

public class List3 extends ListActivity {

List<Map<String, Object>> list;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

list = getListForSimpleAdapter(); //list為資料集合

SimpleAdapter     adapter = new SimpleAdapter(this, list,
R.layout.item,
new String[] { “BigText”, “LittleText”, “img” },
new int[] { R.id.BigText,R.id.LittleText, R.id.img });

//可以想成把一個放好資料的布局檔案放入本介面中
setListAdapter(adapter);

}

private List<Map<String, Object>> getListForSimpleAdapter() {
List<Map<String,Object>> list=newArrayList<Map<String,        Object>>(3);
Map<String, Object> map = new HashMap<String, Object>();
map.put(“BigText”, “Android”);
map.put(“LittleText”, “Google phone.”);
map.put(“img”, R.drawable.n);
list.add(map);

map = new HashMap<String, Object>();
map.put(“BigText”, “Lenovo”);
map.put(“LittleText”, “Ophone”);
map.put(“img”, R.drawable.o);
list.add(map);

map = new HashMap<String, Object>();
map.put(“BigText”, “Droid”);
map.put(“LittleText”, “Motorola”);
map.put(“img”, R.drawable.droid);
list.add(map);

return list;
}
}

//==============================SimpleCursorAdapter===================

注意:SimpleCursorAdapter是和資料庫有關的東西,而sqlite資料庫所要求的 自動成長的 id  必須是 _id這中形式

public class List2 extends Activity {

private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

listView=new ListView(this);
Cursorc=getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
startManagingCursor(c);

ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME} ,
new int[] {android.R.id.text1});
listView.setAdapter(adapter);
setContentView(listView);
}
}

//====================ArrayAdapter===================================

public class List1 extends  Activity {

private ListVeiw listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

listView=new ListVeiw(this);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
setContentVieww(listView);
}

private String[] mStrings = {
“Abbaye de Belloc”, “Abbaye du Mont des Cats”,
“Acorn”, “Adelost”, “Affidelice au Chablis”,
“Aisy Cendre”, “Allgauer Emmentaler”, “Alverca”,
“Ami du Chambertin”, “Anejo Enchilado”, “Anneau
“Aragon”, “Ardi Gasna”, “Ardrahan”, “Armenian
“Asadero”, “Asiago”, “Aubisque Pyrenees”, “Autun”,
“Babybel”, “Baguette Laonnaise”, “Bakers”, “Bal”};
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.