当前位置:操作系统 > 安卓/Android >>

Android 控件 ListView 用法

示例代码:

这是一个读取联系人的代码:


[java]
package com.ui.domain; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.app.Activity; 
import android.database.Cursor; 
import android.database.DataSetObserver; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
 
public class CallPhone extends Activity { 
     
     
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.call_phone); 
        //联系人列表  
        final List<String> dataList=new ArrayList<String>(); 
           // 获得所有的联系人    
        Cursor cur = getContentResolver().query(   
                ContactsContract.Contacts.CONTENT_URI,   
                null,   
                null,   
                null,   
                ContactsContract.Contacts.DISPLAY_NAME   
                        + " COLLATE LOCALIZED ASC");  
        while (cur.moveToNext()) { 
               int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);   
               int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
                
               String phoneNumber=cur.getString(1); 
                
               String disPlayName = cur.getString(displayNameColumn); 
              dataList.add(disPlayName+":"+phoneNumber); 
               Log.i("联系人名称:", disPlayName); 
        } 
        cur.close();//关闭游标  
         
        ListView listView=(ListView)findViewById(R.id.listView1); 
        listView.setAdapter(new ListAdapter() { 
             
            @Override 
            public void unregisterDataSetObserver(DataSetObserver paramDataSetObserver) { 
                // TODO Auto-generated method stub  
                 
            } 
             
            @Override 
            public void registerDataSetObserver(DataSetObserver paramDataSetObserver) { 
                // TODO Auto-generated method stub  
                 
            } 
             
            @Override 
            public boolean isEmpty() { 
                // TODO Auto-generated method stub  
                return false; 
            } 
             
            @Override 
            public boolean hasStableIds() { 
                // TODO Auto-generated method stub  
                return false; 
            } 
             
            @Override 
            public int getViewTypeCount() { 
                // TODO Auto-generated method stub  
                return dataList.size(); 
            } 
             
            @Override 
            public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) { 
                // TODO Auto-generated method stub  
       &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,