Android:如何将此ContentProvider代码移动到外部类?

时间:2012-03-19 01:05:46

标签: android oop android-contacts

我试图保持我的代码整洁,并尽可能将大部分功能移到外部类,以便我可以轻松地重复使用它。

我有一个我正在使用的联系人选择器,并希望将其移动到自己的类中以帮助重用。是否可能,如果是这样,我如何将底部两种方法移到他们自己的类中?

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;
    }

}
}

1 个答案:

答案 0 :(得分:2)

像这样:

public class mainDashView extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
        Something.showContactSelector(this);
    }

    @Override
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        Something.onActivityResult(this, reqCode, resultCode, data);
    }
}

class Something {
    private static final int PICK_CONTACT = 1;

    public static void showContactSelector(Activity activity) {
        Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        activity.startActivityForResult(contactIntent, PICK_CONTACT);
    }

    public static void onActivityResult(Activity activity, int reqCode, int resultCode, Intent data) {
        switch (reqCode) {
            case (PICK_CONTACT):
                if (resultCode == Activity.RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor c = activity.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;
        }
    }
}

您需要传递调用Activity以使某些方法起作用。例如,在您的Activity类中,您可以直接调用startActivityForResult(contactIntent, PICK_CONTACT);,但这只是this.startActivityForResult(contactIntent, PICK_CONTACT);的缩短版本。在另一个类中,您需要通过对类的引用替换不可见的this