Android學習筆記_ListView(列表視圖)(一)

來源:互聯網
上載者:User

標籤:

一、概念。

  ListView是以垂直列表的形式顯示所有清單項目。

二、使用

  假設資料庫中有50個資料資訊(persons,包括name、number、money),通過ListView顯示出來。

  1、向資料庫中50個person資訊。具體添加方式可參考資料庫章節介紹,採用資料庫例子中的dao2的add方法結合for迴圈增加。

添加資料代碼如下:

1 public void testAdd() throws Exception {2         PersonDao2 dao = new PersonDao2(getContext());3         long number = 885900000l;4         Random random = new Random();5         for(int i=0;i<50;i++){6             dao.add("wangwu"+i, Long.toString(number+i),random.nextInt(5000));7         }    8     }
View Code

  2、將資料庫資訊展現至介面。

  (1)採用ListView組件實現。ListView是典型的MVC設計模式。M:mode 資料類型,也就是List集合;V:view 視圖,也就是ListView;C:controller 控制器,adapter 資料配接器。

    ①.在布局(Layout)檔案中使用線性布局(LinearLayout),垂直方向。

    ②.增加ListView控制項,設定id和寬高(填充表單)

    ③.在主代碼中建立並通過findViewById找到ListView控制項;

    ④.採用ListView的setAdapt(adapter(ListAdapter介面))方法。由於參數adaper是介面類型的,所以需要建立介面的實作類別。

    ⑤.建立一個類(MyAdapt)實現ListAdapter介面,並實現介面中為實現的方法。由於ListAdapter介面中為實現的方法比較多,因此建立的類(MyAdapter)只要去繼承ListAdapter介面的簡單實作類別(BaseAdapter),由於BaseAdapter是抽象類別,所以需要實現其中的抽象方法。(規律:如果一個介面的實作類別非常多,一般就會存在BaseXXX、SimpleXXX、DefaultXXX等簡單實作類別,然後只要繼承這些簡單實作類別即可)。

    ⑥.BaseAdapter抽象類別裡的抽象方法。getCount():控制ListView裡面共有多少個條目,也就是從資料庫返回的List集合的條目數量,即條目個數=集合的size;getView(int position, View convertView, ViewGroup parent),擷取到一個view對象,用來展現資料集合某一個位置對應的資料,方法參數position表示當前資料在集合中的位置。

    ⑦.在getView(int position, View convertView, ViewGroup parent)方法中,new出一個TextView對象並通過getApplicationContext()方法放入上下文再設定樣式,然後通過person的get(position)方法得到position位置對應的person對象,再通過position對象的toString()方法獲得String對象並通過TextView對象的setText方法加至TextView對象中,最後該方法返回TextView對象。

通過ListVie組件展示代碼如下:

主代碼:

 1 public class MainActivity extends Activity { 2     private ListView lv; 3     private List<Person> persons; 4  5     @Override 6     protected void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8         setContentView(R.layout.activity_main); 9         LinearLayout ll_root = (LinearLayout) findViewById(R.id.ll_root);10         PersonDao dao = new PersonDao(this);11         persons = dao.findAll();12         13         lv = (ListView) findViewById(R.id.lv);14         lv.setAdapter(new MyAdapter());        15     }16     17     public class MyAdapter extends BaseAdapter{18         private static final String TAG = "MyaAdapter";19         20         @Override21         public int getCount() {22             return persons.size();23         }24 25         @Override26         public Object getItem(int position) {27             // TODO Auto-generated method stub28             return null;29         }30 31         @Override32         public long getItemId(int position) {33             // TODO Auto-generated method stub34             return 0;35         }36 37         @Override38         public View getView(int position, View convertView, ViewGroup parent) {39             Log.i(TAG, "返回View對象,位置:"+position);40             TextView tv = new TextView(getApplicationContext());41             tv.setTextSize(20);42             tv.setTextColor(color.black);43             Person person =  persons.get(position);44             tv.setText(person.toString());45             return tv;46         }        47     }48 }
View Code

layout代碼:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:id="@+id/ll_root" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:orientation="vertical" 7     tools:context=".MainActivity" > 8      9     <ListView 10         android:id="@+id/lv"11         android:layout_height="match_parent"12         android:layout_width="match_parent"></ListView>13 14 </LinearLayout>
View Code

   

  (2)採用非ListView組件實現

    ①.介面定義為線性布局LinearLayout,定義id;

    ②.在主代碼中通過findviewbyid找到該線性布局;

    ③.new出一個PersonDao對象,通過PersonDao對象裡面的findAll()方法得到所有person對象的集合;

    ④.採用for迴圈遍曆person集合;

    ⑤.在for中將每個person對象通過tostring()方法轉成String字串,並new出一個TextView()對象,設定TextView()對象的字型大小、顏色等相關資料,然後用TextView()對象的setText()方法將每個person資訊加至對應的TextView()中。

    ⑥.由於LinearLayout是個View對象的容器,所以可以採用LinearLayout的addView(View chind)方法動態地將每個TextView()對象增加至LinearLayout中。

    ⑦.採用ScrollView對象包裹LinearLayout對象,實現多資料的上下滾動顯示效果。

主程式碼如下:

 1 public class MainActivity extends Activity { 2  3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_main); 7         LinearLayout ll_root = (LinearLayout) findViewById(R.id.ll_root); 8         PersonDao dao = new PersonDao(this); 9         List<Person> persons = dao.findAll();10         for(Person person : persons){11             String info = person.toString(); 12             TextView tv = new TextView(this);13             tv.setTextSize(20);14             tv.setTextColor(Color.BLACK);15             tv.setText(info);16             ll_root.addView(tv);17         }18     }19 }
View Code

Layout代碼如下:

 1 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5  6     <LinearLayout 7         android:id="@+id/ll_root" 8         android:layout_width="match_parent" 9         android:layout_height="match_parent"10         android:orientation="vertical"11         tools:context=".MainActivity" >12     </LinearLayout>13 14 </ScrollView>
View Code

 

Android學習筆記_ListView(列表視圖)(一)

聯繫我們

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