android——ListView功能的实现
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; @Override protected 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 @Override public int getCount() {//获取条目数量 return persons.size(); } @Override public Object getItem(int position) {//获取指定对象 return persons.get(position); } @Override public long getItemId(int position) {//获取指定对象的id return position; } @Override public 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中的TextView TextView 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; } } @Override public 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; } }
-------------------------------------------------------
结果如下所示: