0

我已经被困了好几天了。我有一个活动可以解析 html 并将信息分类到 5 个数组列表中。每个数组列表是一个工作日。我想获取每个数组列表并在单独的页面上的列表视图中显示信息,所以星期一将显示在一个页面上,然后通过滑动移动到星期二等等。

我采用了 eclipse 默认的可滚动标签 + 滑动导航,我正在尝试从那里构建。

所以基本上我想用 5 个数组列表填充 5 页数据,每页 1 个数组列表。任何想法如何在特定页面中将数组列表分配给列表视图?

这是我到目前为止的代码

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 类

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

}

4

1 回答 1

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,并使用此适配器填充列表视图片段。现在您所要做的就是为列表创建 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 库或 support.v4)。

于 2013-04-21T15:08:25.917 回答