1.ListView:它以列表的形式展示具体要显示的内容,并且能够根据数据的长度自适应屏幕显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
2.使用ListView的步骤:
2.1根据ID获取ListView对象,ListView lv = (ListView) findViewById(R.id.lv);
2.2准备适配器,Adapter
class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return list.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) { // TODO Auto-generated method stub Emp e = list.get(position); View v = View.inflate(MainActivity.this,R.layout.item_listview,null); TextView tv_name = (TextView) v.findViewById(R.id.tv_name); tv_name.setText(e.getName()); TextView tv_salary = (TextView) v.findViewById(R.id.tv_salary); tv_salary.setText(e.getSalary()); TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone); tv_phone.setText(e.getPhone()); return v; } }
2.3传入适配器对象,lv.setAdapter(adapter);