我有一个tabBase Activity
和一个Fragment
。我在基地Activity
中调用API,并使用模型列表类将该列表数据传递给Fragment
。但我总是得到零列表。请帮帮我。
public class ListTempStorage {
private List<MyPojoClassList> listOne = new ArrayList<>();
private List<MyPojoClassList> listTwo = new ArrayList<>();
private List<MyPojoClassList> listThree = new ArrayList<>();
public List<MyPojoClassList> getListOne() {
return listOne;
}
public void setListOne(List<MyPojoClassList> listOne) {
this.listOne = listOne;
}
public List<MyPojoClassList> getListTwo() {
return listTwo;
}
public void setListTwo(List<MyPojoClassList> listTwo) {
this.listTwo = listTwo;
}
}
我的Fragment
课程:
public class MyFragment extends Fragment {
private ListTempStorage ourInstance;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_layout, container, false);
// Inflate the layout for this fragment
init(v);
return v;
}
private void init(View v) {
ourInstance= new ListTempStorage ();
try {
List<MyPojoClassList> list = ourInstance.getListOne();
if (list != null && list.size() > 0) {
populateOpenAllLayout(list);
} catch (Exception e) {
if (OlabiConstant.IS_DEBUGGABLE) e.printStackTrace();
activity.finish();
}
答案 0 :(得分:1)
您只会变空,因为在onCreateView
中调用init时总是填充数据:
private void init(View v) {
ourInstance= new ListTempStorage ();
}
您需要在获得数据后填充数据,创建一个方法:
public void populate(ListTempStorage tempStorage) {
ourInstance = tempStorage;
// update your data here.
}
然后,您可以从Base Activity调用方法片段。