Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 控件 ListView 用法

Android 控件 ListView 用法

編輯:關於Android編程

示例代碼:

這是一個讀取聯系人的代碼:


[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  
                EditText text=new EditText(CallPhone.this); 
                text.setBackgroundColor(Color.WHITE); 
                text.setText(dataList.get(paramInt)); 
                return text; 
            } 
             
            @Override 
            public int getItemViewType(int paramInt) { 
                // TODO Auto-generated method stub  
                return 0; 
            } 
             
            @Override 
            public long getItemId(int paramInt) { 
                // TODO Auto-generated method stub  
                return 0; 
            } 
             
            @Override 
            public Object getItem(int paramInt) { 
                // TODO Auto-generated method stub  
                return dataList.get(paramInt); 
            } 
             
            @Override 
            public int getCount() { 
                // TODO Auto-generated method stub  
                return dataList.size(); 
            } 
             
            @Override 
            public boolean isEnabled(int paramInt) { 
                // TODO Auto-generated method stub  
                return false; 
            } 
             
            @Override 
            public boolean areAllItemsEnabled() { 
                // TODO Auto-generated method stub  
                return false; 
            } 
        }); 
         
         
         
       
    } 
 
    @Override 
    protected void onDestroy() { 
        // TODO Auto-generated method stub  
        super.onDestroy(); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // TODO Auto-generated method stub  
        getMenuInflater().inflate(R.menu.call_phone, menu); 
        return true; 
    } 
 
     

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
    EditText text=new EditText(CallPhone.this);
    text.setBackgroundColor(Color.WHITE);
    text.setText(dataList.get(paramInt));
    return text;
   }
   
   @Override
   public int getItemViewType(int paramInt) {
    // TODO Auto-generated method stub
    return 0;
   }
   
   @Override
   public long getItemId(int paramInt) {
    // TODO Auto-generated method stub
    return 0;
   }
   
   @Override
   public Object getItem(int paramInt) {
    // TODO Auto-generated method stub
    return dataList.get(paramInt);
   }
   
   @Override
   public int getCount() {
    // TODO Auto-generated method stub
    return dataList.size();
   }
   
   @Override
   public boolean isEnabled(int paramInt) {
    // TODO Auto-generated method stub
    return false;
   }
   
   @Override
   public boolean areAllItemsEnabled() {
    // TODO Auto-generated method stub
    return false;
   }
  });
  
  
  
     
 }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  getMenuInflater().inflate(R.menu.call_phone, menu);
  return true;
 }

 
}
這個listView有個setAdapter 適配器,裡面可以直接實現接口,或者寫個類,實現[java] view plaincopyprint?ListAdapter 

ListAdapter這個接口

 


[java]
?listView.setAdapter 

listView.setAdapter
在定義一個list 集合 泛型為String

 


接口中有個方法:


[java]
@Override 
        public Object getItem(int paramInt) { 
            // TODO Auto-generated method stub  
            return dataList.get(paramInt); 
        } 

 @Override
   public Object getItem(int paramInt) {
    // TODO Auto-generated method stub
    return dataList.get(paramInt);
   }
return 就是 返回list的數據


paramInt 這是索引號

直接取list.get(索引)

這樣就可以了

還要實現上述的幾個方法

注意,如果list!>1  就會報錯

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved