android学习之ListView布局的学习

  ListView视图是android开发过程中,很常用的一种View组件,该视图将其他控件以Item条目的形式显示在手机屏幕中,而被显示的控件全部来自于Adapter适配器。    

      一个ListView布局的使用与显示,我们可以按照以下方法进行:

     1.  创建带ListView控件的全局布局文件activity_main.xml:

<LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
    xmlns:tools="<a target=_blank href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"
    android:id="@+id/container"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >
    <ListView
       android:id="@+id/phoneListView"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:text="@string/phoneView"
     />
 </LinearLayout>

     2. 创建用来存放ListView布局中具体子项item的显示方式的布局,简单的说就是每个条目需要显示哪些内容,现在我们创建一个线性水平布局,分别用来显示手机联系人的ID和联系人的姓名:现在创建Item布局文件:listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
  
   <TextView
	  android:layout_width="120dp"
	  android:textSize="22sp"
	  android:layout_height="wrap_content"
	  android:id="@+id/name"
   />
 
   <TextView
	  android:layout_width="150dp"
	  android:textSize="22sp"
	  android:layout_height="wrap_content"
	  android:id="@+id/callId"
   />
</LinearLayout>

      3. 创建Adapter适配器对象,下面分别介绍SimpleAdapter, SimpleCursorAdapter和自定义适配器

         首先介绍SimpleCursorAdapter适配器:

	private void showPhoneSimpleCursorAdapter(){
	     List<Map<String,Object>> phoneCall=new ArrayList<Map<String,Object>>();
		
	     ContentResolver phoneResolver=this.getContentResolver();
	     Cursor phoneCursor=phoneResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		
	     SimpleCursorAdapter phoneAdapter=new SimpleCursorAdapter(this, R.layout.listview, phoneCursor, new String[]{"_id","DISPLAY_NAME"}, new int[]{R.id.callId,R.id.name});
		//为ListView对象phoneListView绑定适配器
	     phoneListView.setAdapter(phoneAdapter);
   	}

         在上面的方法中 首先获得ContentResolver 对象,phoneResolver 用来获取系统联系人的Cursor对象;然后在SimpleCursorAdapter构造方法中调用这个Cursor对象,其中SimpleCursorAdapter的构造方法的参数分别表示:Context对象,Item布局的ID,Cursor对象和需要显示的Cussor对象中的字段数组,item布局中的ID数组。

       下面是SimpleAdapter适配器:

	private void showPhoneSimpleAdapeter(){
		List<Map<String,Object>> phoneCall=new ArrayList<Map<String,Object>>();
		
		ContentResolver phoneResolver=this.getContentResolver();
		Cursor phoneCursor=phoneResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while(phoneCursor.moveToNext()){
			HashMap<String,Object> phoneCallInfo=new HashMap<String,Object>();
			phoneCallInfo.put("personId", phoneCursor.getString(phoneCursor.getColumnIndex("_id")));
			phoneCallInfo.put("personName", phoneCursor.getString(phoneCursor.getColumnIndex("DISPLAY_NAME")));
			phoneCall.add(phoneCallInfo);
		}
		
		SimpleAdapter phoneAdapter=new SimpleAdapter(this,phoneCall,R.layout.listview, new String[]{"personId","personName"}, new int[]{R.id.callId,R.id.name});
		
		phoneListView.setAdapter(phoneAdapter);
	}

          最后面2个参数分别表示:Map<String,Object> 中的key组成的数组和Item布局中的控件ID组成的数组,注意2个数组中表示的资源存在一定的对应关系。

          下面我们来介绍一下自定义适配器的开发:

           首先我们定义一个Person类:

  public class Person {
	private String personid;
	private String personNmae;
	public String getPersonid() {
		return personid;
	}
	public void setPersonid(String personid) {
		this.personid = personid;
	}
	public String getPersonNmae() {
		return personNmae;
	}
	public void setPersonNmae(String personNmae) {
		this.personNmae = personNmae;
	}
 }

          接着我们需要创建一个继承了android.widget.BaseAdapter类的自定义适配器类,并重写其Abstract方法,如下:

 

public class SelfAdapter extends BaseAdapter{
	private List<?> mData;
	private LayoutInflater mInflater; //布局加载器,用来加载Item布局,这个对象为必须的对象
	private int selfResource;
	
	public SelfAdapter(Context context,List<?> selfList,int resource){
		mData=selfList;
		mInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//获得布局加载器对象
		selfResource=resource;
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){ //如果是第一页,才加载布局,否则直接调用系统缓存布局
			convertView=mInflater.inflate(selfResource,null); //加载Item布局,
		}
		TextView textId=(TextView)convertView.findViewById(R.id.callId);
		TextView textName=(TextView)convertView.findViewById(R.id.name);
		
		Person nowPerson=(Person)getItem(position);
		textId.setText(nowPerson.getPersonid());
		textName.setText(nowPerson.getPersonNmae());
		
		return convertView;
	}
}

         最后在Activity中创建调用自定义适配器的方法,类似于上面的SimpleAdapter和SimpleCursorAdapter方法:

	public void showSelfAdapter(){
		List<Person> phoneCall=new ArrayList<Person>();
		
		ContentResolver phoneResolver=this.getContentResolver();
		Cursor phoneCursor=phoneResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		while(phoneCursor.moveToNext()){
			Person newPerson=new Person();
			newPerson.setPersonid(phoneCursor.getString(phoneCursor.getColumnIndex("_id")));
			newPerson.setPersonNmae(phoneCursor.getString(phoneCursor.getColumnIndex("DISPLAY_NAME")));
			phoneCall.add(newPerson);
		}
		
		SelfAdapter phoneAdapter=new SelfAdapter(this,phoneCall,R.layout.listview);
		
		phoneListView.setAdapter(phoneAdapter);
	}

        4.在Activity中对ListView对象绑定适配器,部署应用就OK 了:     

    public class MainActivity extends Activity {
	private ListView phoneListView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
		
            phoneListView=(ListView)findViewById(R.id.phoneListView);
	    //showPhoneSimpleCursorAdapter();//调用SimpleCursourAdapter适配器
	    //showPhoneSimpleAdapeter(); //调用SimpleAdapter适配器
	    showSelfAdapter();//调用自定义适配器
	}
     }




 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值