• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小咖博客
博客园    首页    新随笔    联系   管理    订阅  订阅

Android ListView使用(非原创)

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);

 

posted @ 2015-12-23 16:11  小咖博客  阅读(195)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3