我是android的初学者关于这个主题有各种各样的问题,但对我来说并不清楚
我想在我的自定义列表适配器中将我的列表传递给片段 怎么做?
public class MainActivity extends AppCompatActivity {
List<News> newsList;
private void selectItem(int position) {
// update the main content by replacing fragments
if (isOnline()) {
newsList = requestData("http://Mywebsite.com/Ilmnews/api/news/getAllNews");
} else {
Toast.makeText(MainActivity.this, "Network ins't available", Toast.LENGTH_SHORT).show();
}
Fragment fragment = new NewsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentholder, fragment).commit();
}
public static class NewsFragment extends Fragment {
public NewsFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_news, container, false);
ListView newsListView = (ListView) rootView.findViewById(R.id.newsListView);
// newsList is List of my News object List<News>
newsListView.setAdapter(new MYListAdapter(getActivity(), newsList));
return rootView;
}
}
}
其他示例仅适用于不适用于自定义对象的系统数据类型
答案 0 :(得分:0)
public class MainActivity extends AppCompatActivity {
public static List<News> newsList;
}
并在您的片段中使用,如下所示
wsListView.setAdapter(new MYListAdapter(getActivity(), MainActivity.newsList));