1、main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/personLV" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
2、item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/idTV" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:textSize="20sp" /> <TextView android:id="@+id/nameTV" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:padding="10dp" android:textSize="20sp" /> <TextView android:id="@+id/balanceTV" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:padding="10dp" android:textSize="20sp" /> </LinearLayout>
3、MainActivity
package com.njupt.listview;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private ListView personLV;private List<Person> persons;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); personLV = (ListView) findViewById(R.id.personLV);PersonDao dao = new PersonDao(this);persons = dao.queryAll(); personLV.setAdapter(new MyBaseAdapter());//給ListView添加adapter,按照adapter中的方法給ListView添加條目}private class MyBaseAdapter extends BaseAdapter{// 定義Adapter, 把每個Person對象產生一個條目, 將所有條目裝入ListView@Overridepublic int getCount() {//擷取條目數量return persons.size();}@Overridepublic Object getItem(int position) {//擷取指定對象return persons.get(position);}@Overridepublic long getItemId(int position) {//擷取指定對象的idreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// 返回指定位置上的條目, 條目會被自動添加到ListView中View item = View.inflate(getApplicationContext(), R.layout.item, null);// 根據布局檔案建立View(LinearLayout)TextView idTV = (TextView) item.findViewById(R.id.idTV);// 擷取這個新產生的View中的TextViewTextView nameTV = (TextView) item.findViewById(R.id.nameTV);TextView balanceTV = (TextView) item.findViewById(R.id.balanceTV);Person p = persons.get(position);// 根據位置擷取Person對象idTV.setText(p.getId() + "");// 給TextView設定文本nameTV.setText(p.getName());balanceTV.setText(p.getBalance()+"");return item;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
-------------------------------------------------------
結果如下所示: