編輯:高級開發
android手機操作系統中,有許多應用比較復雜,對於初學者來說還需要在實踐中去加深對此的理解。在android ListVIEw的使用較為復雜一點,也就是配置其Adapter,Adapter有幾種,有ArrayAdapter,SimpleAdapter等,首先要生成一個ListView(當然可以使用ListActivity,此Activity整合了ListView)然後用Adapter來設定ListVIEw的顯示數據及布局方式,然後再來響應OnItemClick 事件,或者在ListActivity改寫onListItemClick 響應事件函數。看如下代碼演示了使用ListActivity:
- import Java.util.ArrayList;
- import Java.util.HashMap;
- import Java.util.Map;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.view.VIEw;
- import android.widget.ListVIEw;
- import android.widget.SimpleAdapter;
- public class Test extends ListActivity {
- @Override
- protected void onListItemClick(ListView l, VIEw v, int position, long id) {
- // TODO Auto-generated method stub
- super.onListItemClick(l, v, position, id);
- this.setTitle(this.mModelData.get(position).get("type").toString());
- }
- SimpleAdapter adapter = null;
- private ArrayList<Map<String, Object>> mModelData = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- initModelData();
- adapter = new SimpleAdapter
(this, mModelData, android.R.layout.two_line_list_item,
new String[]{"name"}, new int[]{android.R.id.text1});- this.setListAdapter(adapter);
- //setContentVIEw(R.layout.main);
- }
- public void initModelData()
- {
- mModelData = new ArrayList<Map<String, Object>>();
- Map<String, Object> item = new HashMap<String,Object>();
- item.put("name", "Linux");item.put("type", "OS");
- mModelData.add(item);
- Map<String, Object> item2 = new HashMap<String,Object>();
- item2.put("name", "android");item2.put("type", "Platform");
- mModelData.add(item2);
- Map<String, Object> item3 = new HashMap<String,Object>();
- item3.put("name", "Tomato");item3.put("type", "Fruit");
- mModelData.add(item3);
- }
- }
下面代碼顯示了,使用android ListVIEw + Activity:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.VIEw;
- import android.widget.AdapterVIEw;
- import android.widget.ArrayAdapter;
- import android.widget.ListVIEw;
- import android.widget.AdapterVIEw.OnItemClickListener;
- public class TestStringList extends Activity implements
OnItemClickListener {- @Override
- public void onItemClick(AdapterVIEw
- > arg0,
- VIEw arg1, int arg2, long arg3) {
- this.setTitle("You Click Item:" + String.valueOf(arg2));
- }
- private ListVIEw mListVIEw = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- mListVIEw = new ListVIEw(this);
- mListVIEw.setOnItemClickListener(this);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>
- (this,android.R.layout.simple_list_item_checked,
- new String[]{"ItemA", "ItemB", "ItemC"});
- mListVIEw.setAdapter(adapter);
- this.setContentView(mListVIEw);
- }
- }
android ListVIEw的相關應用就為大家介紹到這裡。
Google於2007年11月5日宣布發布android 手機,這款手機是基於Linux內核的一款操作系統,谷歌將繼續努力,讓這些服務變得更好,同時也將添加更有吸引力的
Google android在推出之後以其開放性而深受好評,應用數量和市場占有率增長幅度明顯。Google android會使Java領域支離破碎?細看Google a
一年一度的“三八婦女節”已經來到了,51CTO移動開發頻道在此向所有的女性同胞致敬,特此為各位找了一些女同胞需要的android手機應用。也希望能夠激發手機應用開發程序
最近,Google面向大學生推出android開發挑戰賽,android開發成為時下開發者的熱點開發項目。像《在NetBeans上搭建android SDK環境》這樣的