Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android ListView復雜使用方式簡單理解

Android ListView復雜使用方式簡單理解

編輯:高級開發

android手機操作系統中,有許多應用比較復雜,對於初學者來說還需要在實踐中去加深對此的理解。在android ListVIEw的使用較為復雜一點,也就是配置其Adapter,Adapter有幾種,有ArrayAdapter,SimpleAdapter等,首先要生成一個ListView(當然可以使用ListActivity,此Activity整合了ListView)然後用Adapter來設定ListVIEw的顯示數據及布局方式,然後再來響應OnItemClick 事件,或者在ListActivity改寫onListItemClick 響應事件函數。看如下代碼演示了使用ListActivity:

  1. import Java.util.ArrayList;
  2. import Java.util.HashMap;
  3. import Java.util.Map;
  4. import android.app.ListActivity;
  5. import android.os.Bundle;
  6. import android.view.VIEw;
  7. import android.widget.ListVIEw;
  8. import android.widget.SimpleAdapter;
  1. public class Test extends ListActivity {
  2. @Override
  3. protected void onListItemClick(ListView l, VIEw v, int position, long id) {
  4. // TODO Auto-generated method stub
  5. super.onListItemClick(l, v, position, id);
  6. this.setTitle(this.mModelData.get(position).get("type").toString());
  7. }
  8. SimpleAdapter adapter = null;
  9. private ArrayList<Map<String, Object>> mModelData = null;
  10. /** Called when the activity is first created. */
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. initModelData();
  15. adapter = new SimpleAdapter
    (this, mModelData, android.R.layout.two_line_list_item,
    new String[]{"name"}, new int[]{android.R.id.text1});
  16. this.setListAdapter(adapter);
  17. //setContentVIEw(R.layout.main);
  18. }
  19. public void initModelData()
  20. {
  21. mModelData = new ArrayList<Map<String, Object>>();
  22. Map<String, Object> item = new HashMap<String,Object>();
  23. item.put("name", "Linux");item.put("type", "OS");
  24. mModelData.add(item);
  25. Map<String, Object> item2 = new HashMap<String,Object>();
  26. item2.put("name", "android");item2.put("type", "Platform");
  27. mModelData.add(item2);
  28. Map<String, Object> item3 = new HashMap<String,Object>();
  29. item3.put("name", "Tomato");item3.put("type", "Fruit");
  30. mModelData.add(item3);
  31. }
  32. }

下面代碼顯示了,使用android ListVIEw + Activity:

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.VIEw;
  4. import android.widget.AdapterVIEw;
  5. import android.widget.ArrayAdapter;
  6. import android.widget.ListVIEw;
  7. import android.widget.AdapterVIEw.OnItemClickListener;
  8. public class TestStringList extends Activity implements
    OnItemClickListener {
  9. @Override
  10. public void onItemClick(AdapterVIEw

  1. > arg0,
  2. VIEw arg1, int arg2, long arg3) {
  3. this.setTitle("You Click Item:" + String.valueOf(arg2));
  4. }
  5. private ListVIEw mListVIEw = null;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. // TODO Auto-generated method stub
  9. super.onCreate(savedInstanceState);
  10. mListVIEw = new ListVIEw(this);
  11. mListVIEw.setOnItemClickListener(this);
  12. ArrayAdapter<String> adapter = new ArrayAdapter<String>
  13. (this,android.R.layout.simple_list_item_checked,
  14. new String[]{"ItemA", "ItemB", "ItemC"});
  15. mListVIEw.setAdapter(adapter);
  16. this.setContentView(mListVIEw);
  17. }
  18. }

android ListVIEw的相關應用就為大家介紹到這裡。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved