0
我試圖保持我的代碼整潔,並儘可能將大部分功能移到外部類,以便我可以輕鬆地重新使用它。Android:如何將此ContentProvider代碼移動到外部類?
我有一個我目前正在使用的聯繫人選擇器,並且希望將其移動到它自己的類中以幫助重用。是否有可能,如果有的話,我該如何將底部的兩種方法轉移到他們自己的課堂上?
public class mainDashView extends Activity {
public static final int PICK_CONTACT = 1;
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
showContactSelector();
}
// How do I move the bottom two Methods into an external class?
private void showContactSelector() {
Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactIntent, PICK_CONTACT);
}
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
}
感謝這個答案 - 我需要對它進行一些修改 - 但它幫助我學到了很多東西,並使我走上正軌。非常感謝! – 2012-05-07 09:03:22