我正在开发一个应用程序并创建了一个 sqlite 数据库、一个自定义适配器以帮助在列表视图中显示它、一个获取输入以填充数据库的活动以及一个带有 viewpager 的活动以在列表视图中滑动。我想要做的是有七页,一周中的每一天一页,并在相应的日子里显示整个数据库。数据库中的字段是课程、日期、开始时间、结束时间和位置。目前我拥有的是一个将所有数据显示到一个页面的应用程序,我无法弄清楚如何根据“日”字段在更多页面之间显示数据。任何建议或教程链接将不胜感激!谢谢。
这是一些代码:
/*main class with viewpager there is an action bar with an action item to
the input activity */
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListView;
public class ScheduleActivity extends Activity {
private Context mContext;
DBSchedule db = new DBSchedule(this);
private ScheduleAdapter scheduleAdapter;
private static final int ENTER_DATA_REQUEST_CODE = 1;
Cursor c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_schedule);
ListView listview1 = new ListView(mContext);
ListView listview2 = new ListView(mContext);
ListView listview3 = new ListView(mContext);
Vector<View> pages = new Vector<View>();
pages.add(listview1);
pages.add(listview2);
pages.add(listview3);
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
PageAdapter adapter = new PageAdapter(mContext,pages);
vp.setAdapter(adapter);
db.open();
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getAllEntries(),CursorAdapter.NO_SELECTION );
listview1.setAdapter(scheduleAdapter);
db.close();
listview2.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A2","B2","C2","D2"}));
listview3.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A3","B3","C3","D3"}));
}
自定义页面适配器
import java.util.List;
import java.util.Vector;
import android.app.Fragment;
import android.content.Context;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class PageAdapter extends PagerAdapter {
private Context mContext;
private Vector<View> pages;
public PageAdapter(Context context, Vector<View> pages) {
this.mContext=context;
this.pages=pages;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View page = pages.get(position);
container.addView(page);
return page;
}
@Override
public int getCount() {
return pages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}