Android ListViewview入門

來源:互聯網
上載者:User

標籤:

接著上文《Android 資料庫的事務》,往person資料表中插入50條資料

    public void testAdd() throws Exception {        PersonDao dao = new PersonDao(getContext());        // .add("wangwu", "123", 50000);        // dao.add("zhangsan", "234", 17000);        int number = 857600001;        Random random = new Random();        for (int i = 0; i < 50; i++) {            dao.add("wuyudong" + i, Long.toString(number + i),                    random.nextInt(5000));        }    }
常規方法顯示資料

首先不使用ListViewview,而是直接使用程式來呈現資料表中的資料,代碼如下:

package com.wuyudong.db;import java.util.List;import com.wuyudong.db.dao.PersonDao;import com.wuyudong.db.domain.Person;import android.os.Bundle;import android.text.style.LeadingMarginSpan;import android.widget.LinearLayout;import android.widget.TextView;import android.app.Activity;import android.graphics.Color;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        LinearLayout ll_root = (LinearLayout)findViewById(R.id.ll_root);        PersonDao dao = new PersonDao(this);        List<Person> persons = dao.findAll();        for (Person person : persons) {            String info = person.toString();            TextView tv = new TextView(this);            tv.setTextSize(20);            tv.setTextColor(Color.BLACK);            tv.setText(info);            ll_root.addView(tv);        }    }}

activity_main.xml中的代碼如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:id="@+id/ll_root"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:context=".MainActivity" >    </LinearLayout></ScrollView>

運行結果如:

使用ListView顯示資料

資料顯示(listview)

需求: 把資料庫的內容全部顯示在介面上

符合MVC模型

m: model 資料模型 -- Person 

v: view 視圖 -- ListView

c: controller 控制器 --Adapter 資料配接器

下面使用ListViewview來實現這個功能

修改activity_main.xml中的代碼如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/ll_root"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </ListView></LinearLayout>

介面如下:

代碼如下:

package com.wuyudong.db;import java.util.List;import com.wuyudong.db.dao.PersonDao;import com.wuyudong.db.domain.Person;import android.os.Bundle;import android.text.style.LeadingMarginSpan;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import android.app.Activity;import android.graphics.Color;public class MainActivity extends Activity {    private ListView lv;    private List<Person> persons;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        PersonDao dao = new PersonDao(this);        persons = dao.findAll();        lv = (ListView) findViewById(R.id.lv);        lv.setAdapter(new MyAdapter());    }    // 預設實作類別形如: simplexxx, basexxx, defaultxxx    private class MyAdapter extends BaseAdapter {        private static final String TAG = "MyAdapter";        /**         * 控制ListView裡面總共有多少條目         */        @Override        public int getCount() {            return persons.size(); // 條目個數==集合的size        }        @Override        public Object getItem(int position) {            // TODO Auto-generated method stub            return null;        }        @Override        public long getItemId(int position) {            // TODO Auto-generated method stub            return 0;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            Log.i(TAG, "返回view對象,位置: " + position);            TextView tv = new TextView(getApplicationContext());            tv.setTextSize(20);            tv.setTextColor(Color.BLACK);            // 得到某個位置對應的person對象            Person person = persons.get(position);            tv.setText(person.toString());            return tv;        }    }}

運行結果如下:

使用logcat過濾器查看:

只顯示5個位置,說明手機螢幕只能顯示這麼多,如果滑動手機螢幕的話,位置數量會越來越多

Android ListViewview入門

聯繫我們

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