标签:android blog http color io os ar java sp
============问题描述============
package com.example.listview; import static java.lang.System.out; 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.support.v4.widget.SimpleCursorAdapter; import android.view.Display; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class Activity01 extends Activity { LinearLayout m_LinearLayout; ListView m_ListView; public void onCreate(Bundle savedInstanceState) { String number = ""; String contact=""; super.onCreate(savedInstanceState); String string = ""; /* 创建LinearLayout布局对象 */ m_LinearLayout = new LinearLayout(this); m_LinearLayout.setOrientation(LinearLayout.VERTICAL); m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK); /* 创建ListView对象 */ 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_ListView到m_LinearLayout布局 */ m_LinearLayout.addView(m_ListView, param); /* 设置显示m_LinearLayout布局 */ setContentView(m_LinearLayout); /* 获取数据库phone的cursor */ ContentResolver cur = getContentResolver(); Cursor cursor = cur.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); startManagingCursor(cursor); while (cursor.moveToNext()) { //logCursor(cursor); int i = 0; i = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); contact = cursor.getString(i); String contactId = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); // 获取联系人的ID号,在SQLite中的数据库ID Cursor phone = cur.query( ContactsContract.CommonDataKinds.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)); // 手机号码字段联系人可能不止一个 number += strPhoneNumber; } string += (contact + ":" + number + "\n"); } ListAdapter adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,(Cursor) cur,new String[]{contact,number},new int[]{android.R.id.text1,android.R.id.text2}); m_ListView.setAdapter(adapter); m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3) { DisplayerToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项"); } public void onNothingSelected(AdapterView<?> arg0) { //没有选中 } }); m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,Long arg3) { DisplayerToast("选中了第"+Integer.toString(arg2+1)+"项"); } }); } public void DisplayerToast(String str) { Toast.makeText(this, str,Toast.LENGTH_SHORT).show(); } }
============解决方案1============
标签:android blog http color io os ar java sp
原文地址:http://www.cnblogs.com/liangxieliang56/p/4033842.html