我什至不确定这会奏效,但这是我们学习的方式,所以不要告诉我这已经坏了:)
我正在尝试我的第一个游标加载器。我认为我正确设置了我的 onCreateLoader 方法,但我不确定如何设置我的 onLoadFinished 和 onLoadReset 方法。这是我第一次使用开关和机箱。任何填写这两种方法的帮助将不胜感激。
此类检索所选联系人 ID 的联系人电话和联系人电子邮件。有趣的是,代码采用联系人电子邮件地址并将其设置为电话号码的文本。
import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
/**
* Created by Pete on 6/19/13.
*/
public class ContactDetails extends Activity implements LoaderCallbacks<Cursor> {
TextView tvContactName, tvPhoneNum, tvPhoneType, tvPhoneFull,
tvEmailAdd, tvEmailType, tvEmailFull,
tvAddress, tvAddType, tvAddFull;
String contactId, contactName, phoneType, phoneFull, phoneNum1,
emailAdd, emailType, emailFull,
address, addType, addFull;
private static final int LOADER_PHONE = 2;
private static final int LOADER_EMAIL = 3;
//Contact List query arguments
Uri uri;
String[] projection, selectionArgs;
String selection, sortOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfo);
initialize();
contactId = getIntent().getStringExtra("contactId");
contactName = getIntent().getStringExtra("contactName");
tvContactName.setText(contactName);
getLoaderManager().initLoader(LOADER_PHONE, null, this);
getLoaderManager().initLoader(LOADER_EMAIL, null, this);
}
public void initialize() {
tvContactName = (TextView)findViewById(R.id.tvContactName);
tvPhoneNum = (TextView)findViewById(R.id.tvPhoneNum);
tvPhoneType = (TextView)findViewById(R.id.tvPhoneType);
tvPhoneFull = (TextView)findViewById(R.id.tvPhoneFull);
tvEmailAdd = (TextView)findViewById(R.id.tvEmailAdd);
tvEmailType = (TextView)findViewById(R.id.tvEmailType);
tvEmailFull = (TextView)findViewById(R.id.tvEmailFull);
tvAddress = (TextView)findViewById(R.id.tvAddress);
tvAddType = (TextView)findViewById(R.id.tvAddType);
tvAddFull = (TextView)findViewById(R.id.tvAddFull);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_PHONE:
uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
projection = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER
};
selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID +
" = " + contactId;
selectionArgs = null;
sortOrder = null;
break;
case LOADER_EMAIL:
uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
projection = new String[] {
ContactsContract.CommonDataKinds.Email.ADDRESS
};
selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID +
" = " + contactId;
selectionArgs = null;
sortOrder = null;
break;
}
return new CursorLoader(this, uri, projection, selection, selectionArgs, sortOrder);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
// Extract the phone number
phoneFull = cursor.getString(
cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
tvPhoneFull.setText(phoneFull);
}
}
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
这是用于设置电子邮件信息的当前注释掉的 while 循环:
uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
projection = new String[] {
ContactsContract.CommonDataKinds.Email.ADDRESS
};
selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID +
" = " + contactId;
selectionArgs = null;
sortOrder = null;
Cursor emailCursor = getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
while (emailCursor.moveToNext()) {
// Extract email address
emailFull = emailCursor.getString(
emailCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Email.ADDRESS));
tvEmailFull.setText(emailFull);
}
emailCursor.close();