總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月23日 星期一

撈出聯絡人資料 ~ if(顯示名稱) ContactsContract.Contacts.DISPLAY_NAME







要讀取聯絡人資料記得Manifest.xml中要加入
加入OnItemClickListener讓user點到的item名稱顯示在Title列



GridViewActivity.java

package com.tsots.GridView_HashMap;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewActivity extends Activity 
{
 GridView g1;
 String str1;
 ArrayList> lstImageItem;
 /** Called when the activity is first created. */  
 @Override  
    public void onCreate(Bundle savedInstanceState) 
 {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
  
  g1 = (GridView) findViewById (R.id.gridview1);
  
  Uri contacts = Contacts.CONTENT_URI;
  //陣列內容: 從contact撈出有顯示名稱的資料
  String[] selectColumn = new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
        Cursor cur = getContentResolver().query(  
                   contacts,  
                   selectColumn,  
                   null,  
                   null,  
                   null
                    );  
        try
        {
         if (cur.moveToFirst()) 
         {                  
             int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
             lstImageItem = new ArrayList>();

                do
                {
                 //用HashMap加入圖片與文字訊息
                 HashMap map = new HashMap();
                 map.put("ItemImage", R.drawable.icon);
                 map.put("ItemText", String.valueOf(cur.getString(displayNameColumn)));
                 lstImageItem.add(map);
                }while(cur.moveToNext());

                SimpleAdapter saImageItems = new SimpleAdapter(this,   
                  lstImageItem,   
                  R.layout.night_item,//ImageView*1, TextView*1  
                  new String[] {"ItemImage","ItemText"},    
                  new int[] {R.id.ItemImage,R.id.ItemText}
                             );
                //每行3欄資料
                g1.setNumColumns(3);
                g1.setAdapter(saImageItems);
             class  ItemClickListener implements OnItemClickListener
             {
              public void onItemClick(AdapterView arg0,//The AdapterView where the click happened   
           View arg1,//The view within the AdapterView that was clicked  
           int arg2,//The position of the view in the adapter  
           long arg3//The row id of the item that was clicked  
                      ) 
              {   
               HashMap item = (HashMap) arg0.getItemAtPosition(arg2);    
               setTitle((String)item.get("ItemText"));
              }
             }
             g1.setOnItemClickListener(new ItemClickListener());
         }
        }
        finally
        {
         cur.close();
        }
 }
}//

沒有留言:

張貼留言