使用单独的arraylists填充5个片段页面

时间:2013-04-21 14:15:43

标签: android

我已经被困了好几天了。我有一个活动将解析HTML并将信息分类为5个arraylists。每个arraylist都是工作日。我想采取每个arraylist并在列表视图中显示单独页面上的信息,因此星期一将显示在一个页面上,然后通过滑动你移动到星期二等等。

我已经使用了eclipse默认的可滚动标签+轻扫导航,我正在尝试从那里构建。

所以基本上我想用5个arraylists填充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) 
    {

    }
}

}

DummySectionFragment Class

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);
}

}

1 个答案:

答案 0 :(得分:1)

你需要做的是标准的,你几乎就在那里。

首先在ViewPager适配器的getItem方法中,启动Fragment以及它所处的位置:

@Override
public Fragment getItem(int i) {
    return DummySectionFragment.newInstance(i);
}

接下来,在Fragment类中,创建一个构造函数来实例化它:

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);
    }
}

Fragment类的上述代码将根据片段的位置获取数组的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>

最后一件事是确保所有Fragment类都来自同一组库(例如,常规Fragment lib或support.v4)。