2012-03-19 68 views
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; 
    } 

} 
} 

回答

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

您需要通過調用活動的一些方法來變通。例如,在您的活動課程中,您可以直接撥打startActivityForResult(contactIntent, PICK_CONTACT);,但這只是this.startActivityForResult(contactIntent, PICK_CONTACT);的縮短版本。在不同的課程中,您需要通過對課程的引用來替換不可見的this

+0

感謝這個答案 - 我需要對它進行一些修改 - 但它幫助我學到了很多東西,並使我走上正軌。非常感謝! – 2012-05-07 09:03:22

相關問題