2014-06-24 22 views
4

我正在开发我的第一个“非教程”应用程序,以增强和增强我的Android开发技能集。newInstance()与自定义Java通用?

我一直在使用大量的Java泛型来增强可重用性和调试,特别是因为我的许多碎片对类Question的子类做了同样的事情。

我刚刚遇到了一个新的传统模式,并且想知道我是否可以将它应用于Java中的泛型类。

根据该文本,newInstance(args,...)方法应当片段类中创建处理的意图附加片段参数的过渡。

实施例:

SomeActivity.class

@​O​v​e​r​r​i​d​e​ 
p​r​o​t​e​c​t​e​d​ ​F​r​a​g​m​e​n​t​ ​c​r​e​a​t​e​F​r​a​g​m​e​n​t​(​)​ ​{​ 
​ ​ ​ ​r​e​t​u​r​n​ ​n​e​w​ ​ObjectF​r​a​g​m​e​n​t​(​)​;​ 

​ ​ ​ ​U​U​I​D​ ​object​I​d​ ​=​ ​(​U​U​I​D​)​g​e​t​I​n​t​e​n​t​(​)​ 
​ ​ ​ ​ ​ ​ ​ ​.​g​e​t​S​e​r​i​a​l​i​z​a​b​l​e​E​x​t​r​a​(Object​F​r​a​g​m​e​n​t​.​E​X​T​R​A​_​OBJECT_​I​D​)​;​ 

​ ​ ​ ​r​e​t​u​r​n​ ​Object​F​r​a​g​m​e​n​t​.​n​e​w​I​n​s​t​a​n​c​e​(object​I​d​)​;​ 
}​ 

ObjectFragment.class

p​u​b​l​i​c​ ​s​t​a​t​i​c​ ObjectF​r​a​g​m​e​n​t​ ​n​e​w​I​n​s​t​a​n​c​e​(​U​U​I​D​ ​object​I​d​)​ ​{​ 
​ ​ ​ ​B​u​n​d​l​e​ ​a​r​g​s​ ​=​ ​n​e​w​ ​B​u​n​d​l​e​(​)​;​ 
​ ​ ​ ​a​r​g​s​.​p​u​t​S​e​r​i​a​l​i​z​a​b​l​e​(​E​X​T​R​A​_​C​R​I​M​E​_​I​D​,​ ​object​I​d​)​;​ 

​ ​ ​ ​ObjectF​r​a​g​m​e​n​t​ ​f​r​a​g​m​e​n​t​ ​=​ ​n​e​w​ ​ObjectF​r​a​g​m​e​n​t​(​)​;​ 
​ ​ ​ ​f​r​a​g​m​e​n​t​.​s​e​t​A​r​g​u​m​e​n​t​s​(​a​r​g​s​)​;​ 

​ ​ ​ ​r​e​t​u​r​n​ ​f​r​a​g​m​e​n​t​;​ 
}​ 

摘自:布赖恩代。 “Android编程:大书呆子牧场指南”。

但是使用Java泛型的情况怎么样?

代码我的工作:

QuestionListActivity.class

public class QuestionListActivity extends SingleFragmentActivity { 


    // CONSTANTS 
    public static final String EXTRA_FRAGMENT_TYPE = "com.renaissanceartsmedia.flashcard.editquestionactivity.fragment"; 
    public static final String EXTRA_ACTIVITY_TITLE = "ListQuestionActivity.EXTRA_ACTIVITY_TITLE"; 
    public static final String TAG = "QuestionListActivity"; 

    // Member Properties 
    QuestionType mFragmentType; 

    @Override 
    protected Fragment createFragment() { 

     mFragmentType = (QuestionType) getIntent().getSerializableExtra(EXTRA_FRAGMENT_TYPE); 
     System.out.println("mFragmentType: " + mFragmentType); 

     // Switch on Enumeration 
     switch (mFragmentType) { 

      case MULTIPLE_ANSWER_QUESTION: 
      case MULTIPLE_CHOICE_QUESTION: 
      case TRUE_FALSE_QUESTION: 

       // PREVIOUS METHOD 
       //return new QuestionListFragment<MultipleAnswerQuestion>(); 

       // Attempting to refactor to newInstance(Bundle args) 
       return QuestionListFragment<MultipleAnswerQuestion>.newInstance(getIntent().getExtras()); // ERROR 

      case MATCHING_QUESTION: 
       return new QuestionListFragment<MatchingQuestion>(); 

      case BLANK_QUESTION: 
       //return new BQFragment(); 
       return new QuestionListFragment<BlankQuestion>(); 
      default: 
       return new QuestionListFragment<Question>(); 
     } 

    } 
} 

目前,我从QuestionListFragment的onCreate()方法中得到的其他方案。我知道如果转换到newInstance()约定应该与Java Generics一起使用,我将删除此代码。

QuestionListFragment.class

public class QuestionListFragment<E extends Question> extends ListFragment implements QuestionDialogInterface { 

     // Constants 
     public static final String TAG = "QuestionListFragement"; 
     public static final String DIALOG_TITLE = "QuestionListFragment.DIALOG_TITLE"; 
     public static final String DIALOG_MESSAGE = "QuestionListFragment.DIALOG_MESSAGE"; 
     public static final String QUESTION_TYPE = "QUESTION_TYPE"; 
     private static final int DIALOG_FRAGMENT = 1; 

     // Member Properties 
     Flashcard mFlashcard; 
     QuestionType mQuestionType; 
     String mActivityTitle; 
     ArrayList<E> mQuestions; 
     DialogFragment mDialogFragment; 

// SOMETHING LIKE THIS??? 
     @SuppressWarnings({ "unchecked", "rawtypes" }) 
     public static QuestionListFragment<? extends Question> newInstance(Bundle args) { 
      // Create a new instance of QuestionListFragment<? extends Question> 
      QuestionListFragment<? extends Question> fragment = new QuestionListFragment(); 

      // Set the arguments 
      fragment.setArguments(args); 

      // Return the Fragment 
      return fragment; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      Log.d(TAG,"Enter onCreate(Bundle savedInstanceState)"); 
      // Enable Options Menu 
      setHasOptionsMenu(true); 

      // Create the ActionBar 'UP' Button 
      getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); 

      // The Intent Extras 
      Bundle extras = getActivity().getIntent().getExtras(); 

      // Extract the Flashcard from the extras 
      UUID flashcardId = (UUID) extras.getSerializable(Flashcard.EXTRA_FLASHCARD_ID); 
      mFlashcard = FlashcardStore.get(getActivity()).getFlashcard(flashcardId); 
      mQuestionType = (QuestionType) extras.getSerializable(EditQuestionActivity.EXTRA_FRAGMENT_TYPE); 
      mActivityTitle = extras.getString(QuestionListActivity.EXTRA_ACTIVITY_TITLE); 


      // Get a Container of Multiple Answer Questions 
      mQuestions = (ArrayList<E>) mFlashcard.getQuestions(mQuestionType); 

      // Set the Title of the Fragment's Activity 
      getActivity().setTitle(mActivityTitle); 

      // Create a list 
      ListItemLayoutAdapter adapter = new ListItemLayoutAdapter(mQuestions); 

      // Set the adapter for the list 
      setListAdapter(adapter); 
      Log.d(TAG,"Exit onCreate(Bundle savedInstanceState)"); 
     } 
    .... 
    } 

什么是最佳实践关于Android的碎片和Java泛型?有人可以描述他们是什么,为什么他们应该使用。如果的newInstance()应使用,请帮我修复错误,提供正确的语法来声明:

// Attempting to refactor to newInstance(Bundle args) 
return QuestionListFragment<MultipleAnswerQuestion>.newInstance(getIntent().getExtras()); // ERROR 
+0

try:return QuestionListFragment。 newInstance(getIntent()。getExtras()); – maress

+0

我得到:此行有多个标记 \t - QuestionListFragment类型的newInstance(Bundle)方法不是通用的;它不能用参数参数化 \t \t - 有关标记的语法错误,请删除这些标记 – Matt

+0

如何使newInstance(Bundle)具有通用性? – Matt

回答

6
public static <T extends Question> QuestionListFragment<T> newInstance(Bundle args) { 
     // Create a new instance of QuestionListFragment<? extends Question> 
     QuestionListFragment<T> fragment = new QuestionListFragment<T>(); 

     // Set the arguments 
     fragment.setArguments(args); 

     // Return the Fragment 
     return fragment; 
    } 

,然后把它称为:

QuestionListFragment.<MultipleAnswerQuestion>newInstance(getIntent().getExtras()‌​); 
+0

只好因错误而稍微改变:public static QuestionListFragment <?扩展问题> newInstance(Bundle args) - 这可行,但我得到一个'标记的语法错误,删除这些令牌的Eclipse Error QuestionListActivity.class – Matt

+0

我纠正了错误。它现在应该工作 – maress

+0

您的编辑在QuestionListFragment.class中工作,但我仍然有'令牌上的语法错误,删除这些令牌'Eclipse的QuestionListActivity.class错误,希望它给了我更多的信息.​​..甚至没有通知我哪个令牌 – Matt

0
public static <T extends BaseFragment> T getInstance (Class<T> mClass, Bundle args) { 
    try { 
     T instance = mClass.newInstance(); 
     instance.setArguments(args); 
     return instance; 
    } catch (java.lang.InstantiationException | IllegalAccessException e) { 
     /** 
     * Error thrown 
     */ 
    } 

    return null; 
} 

较短版。 Cheers