Bin's

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package org.ben.test;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AndroidTest_ListViewActivity extends Activity {

//LinearLayout是ListView的容器
LinearLayout m_LinearLayout;
ListView m_ListView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建LinearLayout布局对象
m_LinearLayout = new LinearLayout(this);
m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);
m_ListView = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
m_ListView.setBackgroundColor(Color.BLACK);

m_LinearLayout.addView(m_ListView,param);

setContentView(m_LinearLayout);

ContentResolver cr = getContentResolver();
// String str1="" ;
// String str2="" ;
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,null);
// startManagingCursor(cursor);
//
// while(cursor.moveToNext())
// {
// int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
// String name = cursor.getString(nameFieldColumnIndex);
// str1+=name;
//
// String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Cursor phone = cr.query(ContactsContract.CommonDaiangtaKinds.Phone.CONTENT_URI, null,
// ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,
// null, null);
//
// while (phone.moveToNext())
// {
// String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// str2 += (":" + strPhoneNumber);
// }
// phone.close();
// }



ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
cursor,
//android2.0以后获取联系人号码都必须使用ContactsContract.CommonDataKinds.Phone.NUMBER

new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER},
new int[] { android.R.id.text1, android.R.id.text2});

m_ListView.setAdapter(adapter);

m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
DisplayToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项");
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//表示没有选中
}
});

m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
DisplayToast("选中了第"+Integer.toString(arg2+1)+"项");
}
});
}

public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
posted on 2011-11-06 22:48  Jesuca  阅读(336)  评论(0编辑  收藏  举报