填充Android联系人

时间:2011-11-25 17:39:48

标签: android contacts

我想从列表视图中选择联系人(似乎有效) - 并在另一个ui中填充所选联系人(然后通过短信将生成的消息发送给所选联系人)。 populateField方法不起作用。

这里是我的代码片段:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow"
    android:orientation="vertical" >




    <TextView
        android:id="@+id/label_sms_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label_sms_number"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />




    <TextView
        android:id="@+id/display_sendSms_name_contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />




    <TextView
        android:id="@+id/display_sendSms_contact_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />




    <TextView
        android:id="@+id/label_sms_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label_sms_message"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />


    <EditText
        android:id="@+id/input_sms_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>



    <Button
        android:id="@+id/button_sms_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label_sms_button_send" />


</LinearLayout>

我的java代码:

public class SendSMS extends Activity{

    private IShoppingListDao shoppingManager;
    private TextView mContact;
    private TextView mMessage; 
    private Long mRowId;
    private static final String TAG = "SendSMS";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_list_by_sms);

        shoppingManager = new ShoppingListDao(this); 
        shoppingManager.open();

        mContact = (TextView) findViewById(R.id.display_sendSms_name_contact);

        mRowId = null;
        Bundle extras = getIntent().getExtras();
        mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
                .getSerializable(ContactsContract.Contacts.DISPLAY_NAME);

        Log.d(TAG, "Row-id: "+mRowId); 

        if (extras != null) {
            mRowId = extras.getLong(ContactsContract.Contacts.DISPLAY_NAME);
        }

        mMessage = (TextView) findViewById(R.id.input_sms_message);
        mMessage.setText(generateMessage());
        populateFields(); 
    }

    private void populateFields() {

        Log.d(TAG,"Entering populateFields"); 
        Log.d(TAG,"Row-id: "+mRowId); 
        if (mRowId != null) {

            ContentResolver cr = getContentResolver();
            String where = ContactsContract.Contacts._ID + " = " + mRowId;
            Log.d(TAG,"String where: "+where); 

            //selecting the name
            Cursor listItem = cr.query(
                    ContactsContract.Contacts.CONTENT_URI, 
                    null, 
                    where, 
                    null, null);

            startManagingCursor(listItem);          
            mContact.setText(listItem.getString(
                    listItem.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        }
    }

0 个答案:

没有答案