Android 使用单独的ArrayList填充5个片段页面
这件事我已经耽搁了好几天了。我有一个活动,将解析html并将信息排序为5个ArrayList。每个arraylist都是一个工作日。我希望获取每个arraylist,并在单独的页面上的列表视图中显示信息,因此周一将显示在一个页面上,然后通过滑动移动到周二,依此类推 我使用了eclipse默认的可滚动标签+滑动导航,并尝试从中构建 所以本质上我想用5个arraylist填充5页数据,每个页面1个arraylist。知道如何在特定页面中将arraylist分配给listview吗 这是我到目前为止的代码Android 使用单独的ArrayList填充5个片段页面,android,Android,这件事我已经耽搁了好几天了。我有一个活动,将解析html并将信息排序为5个ArrayList。每个arraylist都是一个工作日。我希望获取每个arraylist,并在单独的页面上的列表视图中显示信息,因此周一将显示在一个页面上,然后通过滑动移动到周二,依此类推 我使用了eclipse默认的可滚动标签+滑动导航,并尝试从中构建 所以本质上我想用5个arraylist填充5页数据,每个页面1个arraylist。知道如何在特定页面中将arraylist分配给listview吗 这是我到目前为止的
public class DisplayOnlineTimetable extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
String value;
Document doc = null;
private ListView mainListView ;
static ViewGroup mViewGroup;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_display_online_timetable_dummy);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_online_timetable, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return DummySectionFragment.newInstance(i);
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
//monday
case 0:
return getString(R.string.title_section1).toUpperCase(l);
//tuesday
case 1:
return getString(R.string.title_section2).toUpperCase(l);
//wednesday
case 2:
return getString(R.string.title_section3).toUpperCase(l);
//thursday
case 3:
return getString(R.string.title_section3).toUpperCase(l);
//friday
case 4:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public void createTimetable(ArrayList<SingleClass> list, Elements elements, Day day)
{
}
private class CreateTimetables extends AsyncTask<URL, Integer, Long>
{
protected Long doInBackground(URL... urls) {
}
protected void onPostExecute(Long result)
{
}
}
公共类DisplayOnlineTimetable扩展了FragmentActivity{
分段SPAGERADAPTER mSectionsPagerAdapter;
/**
*将承载节内容的{@link ViewPager}。
*/
字符串值;
单据单据=空;
私有ListView主ListView;
静态视图组mViewGroup;
ViewPager mViewPager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment\u display\u online\u timeline\u dummy);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//应用程序的主要部分。
mSectionsPagerAdapter=新节spageradapter(
getSupportFragmentManager());
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.display\u online\u时间表,菜单);
返回true;
}
/**
*一个{@link FragmentPagerAdapter},返回对应于
*其中一个部分/选项卡/页面。
*/
公共类节SpagerAdapter扩展了FragmentPagerAdapter{
公共部分SpagerAdapter(碎片管理器fm){
超级(fm);
}
@凌驾
公共片段getItem(int i){
返回DummySectionFragment.newInstance(i);
}
@凌驾
public int getCount(){
返回5;
}
@凌驾
公共字符序列getPageTitle(int位置){
Locale l=Locale.getDefault();
开关(位置){
//星期一
案例0:
返回getString(R.string.title_section1).toUpperCase(l);
//星期二
案例1:
返回getString(R.string.title_section2.toUpperCase(l);
//星期三
案例2:
返回getString(R.string.title_section3).toUpperCase(l);
//星期四
案例3:
返回getString(R.string.title_section3).toUpperCase(l);
//星期五
案例4:
返回getString(R.string.title_section3).toUpperCase(l);
}
返回null;
}
}
公共无效创建时间表(ArrayList、元素、日期)
{
}
私有类CreateTimetables扩展了AsyncTask
{
受保护的长doInBackground(URL…URL){
}
受保护的void onPostExecute(长结果)
{
}
}
}
Dummy片段类
public class DummySectionFragment extends ListFragment {
private Integer arrayListId;
ViewGroup myViewGroup;
public static final String CATEGORY_POSITION = "section_number";
public static DummySectionFragment newInstance(int pos) {
DummySectionFragment f = new DummySectionFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt(CATEGORY_POSITION, pos);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
//get the id for your array list
arrayListId = getArguments() != null ? getArguments().getInt(CATEGORY_POSITION) - 1 : 1;
}
//create the list view layout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myViewGroup = container;
View v = inflater.inflate(R.layout.list_row, container, false);
return v;
}
//populate the list view row
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<SingleClass> arrayAdapter =
new ArrayAdapter<SingleClass>(getActivity(), android.R.id.list, R.layout.list_row);
setListAdapter(arrayAdapter);
}
公共类DummySectionFragment扩展了ListFragment{
私有整数arrayListId;
视图组myViewGroup;
公共静态最终字符串CATEGORY\u POSITION=“section\u number”;
公共静态DummySectionFragment newInstance(int pos){
DummySectionFragment f=新的DummySectionFragment();
//提供num输入作为参数。
Bundle args=新Bundle();
参数putInt(类别位置,位置);
f、 设置参数(args);
返回f;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
//获取数组列表的id
arrayListId=getArguments()!=null?getArguments().getInt(类别位置)-1:1;
}
//创建列表视图布局
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
myViewGroup=容器;
视图v=充气机。充气(R.layout.list_行,容器,错误);
返回v;
}
//填充列表视图行
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayAdapter ArrayAdapter=
新的ArrayAdapter(getActivity(),android.R.id.list,R.layout.list_行);
setListAdapter(arrayAdapter);
}
}你需要做的是标准化的,你就快做到了 首先,在ViewPager适配器的getItem方法中,启动其所在位置的片段:
@Override
public Fragment getItem(int i) {
return DummySectionFragment.newInstance(i);
}
接下来,在片段类中,创建一个构造函数来实例化它:
public static DummySectionFragment newInstance(int pos) {
DummySectionFragment f = new DummySectionFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt(CATEGORY_POSITION, pos);
f.setArguments(args);
return f;
}
现在,让您的DummySectionFragment扩展ListFragment,并在onActivityCreated方法中填充它:
public class DummySectionFragment extends ListFragment {
private Integer arrayListId;
//the constructor from above
@Override
public void onCreate(Bundle savedInstanceState) {
//get the id for your array list
arrayListId = getArguments() != null ? getArguments().getInt(CATEGORY_POSITION) - 1 : 1;
}
//create the list view layout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myViewGroup = container;
View v = inflater.inflate(R.layout.fragment_list, container, false);
return v;
}
//populate the list view row
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this, android.R.layout.list, <your_array_list_row.xml>);
setListAdapter(adapter);
}
}
公共类DummySectionFragment扩展了ListFragment{
私有整数arrayListId;
//上面的构造函数
@凌驾
创建时的公共void(Bundle savedInstanceState){
//获取数组列表的id
arrayListId=getArguments()!=null?getArguments().getInt(类别位置)-1:1;
}
//创建列表视图布局
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
myViewGroup=容器;
视图v=充气机。充气(R.layout.fragment_列表,容器,错误);
返回v;
}
//填充列表视图行
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ArrayAdapter ArrayAdapter=
新的ArrayAdapter(这个,android.R.layout.list,);
setListAdapter(适配器);
}
}
上面的片段类代码将根据片段的位置获取数组的id,并为其创建ArrayAdapter,并使用此适配器填充listview片段。现在,您只需为列表创建xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>