Android ListView中Adapter、填充器的使用

直接上代码:


package com.example.listviews;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
	ListView lv;
	String [] str = {"111","222","333","444","555","666","777","888","999","1010","qqqq","666","777","888","999","1010","qqqq"};
	List<User> list = new ArrayList<User>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		for(int i = 1; i <= 20; i++){
			list.add(new User(""+i, "测试"+i, "138857955"+i));
		}
		lv = (ListView) findViewById(R.id.lv);
		//方法1:自己定义Adapter
		lv.setAdapter(new MyAdapter());
		
		//方法2:使用系统自带Adapter ArrayAdapter适用数据单一界面简单的布局
//		lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list, R.id.id, str));
//		
//		//方法2:使用系统自带Adapter SimpleAdapter适用数据相对复杂的布局
//		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
//		Map<String,Object> map = new HashMap<>();
//		map.put("id", "id:111");
//		map.put("name", "name:zhangsan");
//		map.put("phone", "phone:13585457754");
//		Map<String,Object> map1 = new HashMap<>();
//		map1.put("id", "id:222");
//		map1.put("name", "name:lisi");
//		map1.put("phone", "phone:1345555555");
//		Map<String,Object> map2 = new HashMap<>();
//		map2.put("id", "id:333");
//		map2.put("name", "name:wangwu");
//		map2.put("phone", "phone:1888888888");
//		listMap.add(map);
//		listMap.add(map1);
//		listMap.add(map2);
//		//参数1:Context 参数2:传入集合对象 参数3:填充的布局XML	参数4:map对象中的key	参数5:布局文件中各个组件的id
//		lv.setAdapter(new SimpleAdapter(this, listMap, R.layout.list, new String[]{"id","name","phone"},new int[]{R.id.id,R.id.name,R.id.phone}));
		
	}
	
	/**
	 * 自定义Adapter
	 * @author Lenovo
	 *
	 */
	public class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			//需要遍历集合的size
			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
			//View.inflate对布局XML文件进行填充,获得View对象(相当于这个R.layout.activity_main)
			//参数1:容器的Context 参数2:表示填充的哪个布局文件XML 
			//参数3:表示具体填充到哪个容器中,这里由Adapter指定,所以填null
			View tv = View.inflate(MainActivity.this,R.layout.list, null);
			// tv.findViewById(R.id.id))获得填充后的布局文件中的具体哪个ID的对象,并赋值
			((TextView)tv.findViewById(R.id.id)).setText("id:"+list.get(position).getId());
			((TextView)tv.findViewById(R.id.name)).setText("name:"+list.get(position).getName());
			((TextView)tv.findViewById(R.id.phone)).setText("phone:"+list.get(position).getPhone());
			return tv;
		}
		
	}
	
	
}

package com.example.listviews;

public class User {

	String id;
	String name;
	public User(String id, String name, String phone) {
		this.id = id;
		this.name = name;
		this.phone = phone;
	}
	String phone;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>
list.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/id"
        android:layout_width="64dp"
        android:layout_height="32dp"
        android:paddingTop="10dp"
        android:paddingLeft="10dp"
        android:text="id:" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:" />

        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:" />

    </LinearLayout>

</LinearLayout>
点击下载源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值