setListAdapter()不起作用

时间:2013-07-07 09:11:55

标签: java android android-contentprovider contact

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Uri allContacts = Uri.parse("content://contacts/people");
            Cursor c = managedQuery(allContacts, null, null, null, null);
            String[] columns = new String[] {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID};
            int[] views = new int[] {R.id.contactName, R.id.contactID};
            SimpleCursorAdapter adapter =
            new SimpleCursorAdapter(this, R.layout.activity_main, c, columns,views);    
            this.setListAdapter(adapter);
    }

}

它总是显示此错误:方法setListAdapter(SimpleCursorAdapter)未定义类型MainActivity

2 个答案:

答案 0 :(得分:4)

要使用setListAdapter,您的班级必须延长ListActivity而不是Activity。变化

public class MainActivity extends Activity

public class MainActivity extends ListActivity 

答案 1 :(得分:0)

更改后使用扩展ListActivity扩展Activity,如果问题尚未结束,您可以尝试 变化:

import android.widget.SimpleCursorAdapter;

使用:

import android.support.v4.widget.SimpleCursorAdapter;