2017-02-27 67 views
0

我有一个ViewPager的主要活动。我正尝试使用适配器填充ViewPager。Android片段onCreateView尽管初始化为null ArrayList尽管初始化

这是我的片段类。

public class ClassListFragment extends Fragment { 

private List<item_timetable_class> _classList; 

public ClassListFragment() { 
} 

public ClassListFragment SetClassList (List<item_timetable_class> classList) { 
    ClassListFragment fragment = new ClassListFragment(); 
    fragment._classList = classList; 
    return fragment; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    // some code here 

    //In this line I am getting _classList = Null   

     class_card_adapter adapter = new class_card_adapter(_classList, getContext()); 

    // some code here 
    } 

} 

这就是我所说的片段。请注意,在创建一个新的ClassListFragment对象之后,我调用SetClassList,以便内部变量_classList被设置。

// Set up the ViewPager with the sections adapter. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    for (int i = 0; i < weekday_list.size(); i++) { 
     ClassListFragment newFragment = new ClassListFragment(); 
     newFragment.SetClassList(classCardList); 
     mSectionsPagerAdapter.addFragment(newFragment, weekday_list.get(i)); 
     i++; 
    } 

    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

不幸的是,在主活动中正在执行下面一行时正在执行onCreateView方法。

mViewPager.setAdapter(mSectionsPagerAdapter); 

而在那个时候,List _classList是空的。

可能是什么问题以及如何解决该问题?

回答

0

你正在创建ClassListFragment

ClassListFragment newFragment = new ClassListFragment(); 

然后不守参考从SetClassList返回片段:

newFragment.SetClassList(classCardList); 

修改SetClassList

public ClassListFragment SetClassList (List<item_timetable_class> classLis) 
{ 
    _classList = classList; 
    return this; 
} 

并保持参考返回Fragment

newFragment = newFragment.SetClassList(classCardList); 
0

请参考:Best practice for instantiating a new Android Fragment

(做你想做什么正确的方法是使用BundleParcelable物体进入分片参数)


反正你叫两个独立“构造函数”在这里。

ClassListFragment newFragment = new ClassListFragment(); 
newFragment.SetClassList(classCardList); 

public ClassListFragment SetClassList回报一个new ClassListFragment,它不设置当前之一。


试试这个。

public static ClassListFragment newInstance(List<item_timetable_class> classList) { 
    ClassListFragment fragment = new ClassListFragment(); 
    fragment._classList = classList; 
    return fragment; 
} 

而且

for (int i = 0; i < weekday_list.size(); i++) { 
    mSectionsPagerAdapter.addFragment(
     ClassListFragment.newInstance(classCardList), 
     weekday_list.get(i)); 
    // i++; // You already increment this in the loop... 
} 
+0

你的答案是更多的信息!谢谢,但R.Zagórski的回答涉及最少量的代码更改。谢谢,你们都很棒! – BuggyCoder

0

我会做的是这样的:

public static ClassListFragment newInstance(final List<item_time_table_class> classList) { 
ClassListFragment fragment = new ClassListFragment(); 
Bundle args = new Bundle() 
args.putParcelable(key, classList); 
fragment.setArguments(args); 
return fragment; 
} 

ClassListFragment::onCreate() { 
classList = getArguments().getParcelable(key); 
}