package com.example.chapter4_ui;import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;public class SimpleCursorAdapters extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//获得通讯录联系人游标对象Cursor
Cursor c=getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
//实例化列表适配器
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME},
new int[] {android.R.id.text1});
setListAdapter(adapter);
}}
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;public class SimpleCursorAdapters extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//获得通讯录联系人游标对象Cursor
Cursor c=getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
//实例化列表适配器
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME},
new int[] {android.R.id.text1});
setListAdapter(adapter);
}}
解决方案 »
- SurfaceView 轮流绘制三张图片
- 如何获取EditText里面的图片内容,然后使其能在另外一个EditText里面也能显示
- android程序调用googlemap 卫星图于底层图错位,求人指教
- android keyguadr锁屏程序不能全屏显示
- 我DDMS 中的菜单让我不小心关了 怎么让他们显示出来啊
- 被一个简单的问题整蒙了:如何实现画布上任意两点间自动的沿直线运动?
- Android setOnClickListener
- 百度地图 地图标注 第二次进入activity 空指针异常
- 求教!!ubuntu下下载android源码的方法
- android。如何查看/获取U盘设备的名称?
- android2.1 Gallery3D的问题
- 关于android4.0系统默认屏幕方向的问题
使用logcat查看错误的源头
<uses-permission android:name="android.permission.READ_CONTACTS" />
1、onCreate中不宜做阻塞的数据查询
2、数据查询应该是异步的
3、联系人数据查询没必要查询所有数据,根据需求,看你下文只需要一个name,那么查询语句应该是
Cursor c=getContentResolver().query(People.CONTENT_URI, new String[]{People.NAME}, null, null, null);
我按照你的第三条改了下,结果又不能运行了,第二个参数还是的改成null但是运行出来没有显示任何内容,不知道是为什么
我看的《Android核心技术与实例详解》这本书,书上有一些例子,我就照着敲,不知道怎么学起,于是就抓了一本书看呢~
我就是在Android手机上运行的啊、、、
1. 没有加载layout
ListActivity需要一个id为@android:list 的 ListView
所以需要新建一个布局文件如下:(以文件名为activity_main.xml为例)<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
然后在super.onCreate(savedInstanceState);下加上
setContentView(R.layout.activity_main); //参数改为自己布局文件的ID2. 2.x版本安卓通讯录数据库的结构比例子中的复杂
例子中的android.provider.Contacts.People 类已经不存在了
所以获得通讯录联系人Cursor的语句改为
Cursor c=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
实例化列表适配器的语句改为
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
new int[] {android.R.id.text1});解决如上两个问题已经可以看见联系人了 但是还有问题3. Cursor对象没有释放 要在重载的onDestory()中调用c.close();方法4. startManagingCursor(Cursor c)方法 和 SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)构造函数 都是被弃用的
如4楼所说 因为这两个方法都是是阻塞的 需要用 CursorLoader进行异步的查询 否则将会导致UI的无响应 甚至ANR错误
MainActivity.java
package com.examples.listacrivity;//打错字了。。 t和r太近了什么的=。=import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;public class MainActivity extends ListActivity {
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得通讯录联系人游标对象Cursor
c=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
//实例化列表适配器
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
new int[] {android.R.id.text1});
setListAdapter(adapter);
}
public void onDestory(){
super.onDestroy();
c.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}res/layout/activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
如果是使用的listviewactivity带的呢,你确实要按照22楼的做些更正!
现在终于可以运行出结果啦!!!!
但是加了setContentView()之后反而无法运行了,去了这句才能运行,不知道为嘛呢~
之前貌似是因为你说的那个People过期了好啦~都怪我看的书版本太老了~~~
谢谢你咯~~~终于解决了~弄了快大半个月了~菜鸟伤不起噢~
照着书敲了,然后不懂的类和方法我都会查API这样可以么~~~