将数据插入内容提供程序,然后将其显示在列表视图中

时间:2011-03-22 03:43:09

标签: android android-emulator android-contentprovider

当我运行模拟器时,我仍然有强制关机问题。我做错了什么?

package fypj.ContactList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Contacts.People;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ContactList extends ListActivity {
    TextView ContactsTV;
    ListView ContactsLV;
    Uri phoneUri = null;
    Uri emailUri = null;
    Uri uri;
    ContentValues values = new ContentValues();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ContactsTV = (TextView)findViewById(R.id.txt);
        ContactsLV = (ListView)findViewById(R.id.list);
        //populateContactList();
        insert(uri, values);
        //retrieve contracts and bind them to list view
        String [] projection = new String[]
        {Phone.DISPLAY_NAME};
        ContactsTV.setText(ContactsContract.PhoneLookup.CONTENT_FILTER_URI.toString());
        int[] to = new int[]{R.id.txtName};

        Cursor managedCursor = managedQuery(phoneUri, projection, null, null, null);
        ListAdapter sca = new SimpleCursorAdapter
        (this, R.layout.listrow, managedCursor,projection, to);
        ContactsLV.setAdapter(sca);
    }

    public final Uri insert(Uri uri, ContentValues values){
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        values.put(Phone.DISPLAY_NAME, "Jaslyn");   
        values.put(Phone.LABEL, "Jaslyn Goh");
        values.put(Phone.STARRED, 1);
        uri = getContentResolver().insert(Data.CONTENT_URI, values);


        phoneUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);

        values.clear();
        values.put(Phone.TYPE, Phone.TYPE_MOBILE);
        values.put(Phone.NUMBER, "91289161");
        return getContentResolver().insert(phoneUri, values);
        //emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY );
        //values.clear();
        //values.put(People.ContactMethods.KIND, Contacts.KIND_EMAIL);
        //values.put(People.ContactMethods.DATA, "asd@hotmail.com");
        //values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
        //getContentResolver().insert(emailUri, values); 

    }

1 个答案:

答案 0 :(得分:1)

package fypj.contacts;

import fypj.e.R;
import android.app.ListActivity; 
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor; 
import android.net.Uri;
import android.os.Bundle; 
import android.provider.Contacts.People; 
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone; 
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.widget.*;  

public class Contacts extends ListActivity  {
    private SimpleCursorAdapter myAdapter;     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String account_type = null;
        String account_name = null;

        ContentValues values = new ContentValues(); 
        values.put(RawContacts.ACCOUNT_TYPE, account_type); 
        values.put(RawContacts.ACCOUNT_NAME, account_name); 
        Uri rawContactUri = 
        getContentResolver().insert(RawContacts.CONTENT_URI, values); 
        long rawContactId = ContentUris.parseId(rawContactUri); 


        values.clear(); 
        values.put(Data.RAW_CONTACT_ID, rawContactId); 
        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); 
        values.put(StructuredName.DISPLAY_NAME, "S.M"); 
        values.put(Phone.LABEL, "Sarah");
        values.put(Phone.NUMBER, "91111111");
        //values.put(ContactsContract.Data.DATA3, "View Contact");
        getContentResolver().insert(Data.CONTENT_URI, values); 


        //contentResolver = getContentResolver(); 



        //Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);     
        Cursor cursor = getContentResolver().query(Data.CONTENT_URI, null, null, null, null);         
        startManagingCursor(cursor);          
        // String[] column = new String[] {Phone.LABEL}; 
        String[] column = new String[] {Phone.LABEL};
        int[] names = new int[] {R.id.contact_name};         
        myAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, column, names);         
        setListAdapter(myAdapter);     
    } 
}