我正在实现一个自定义水平视图寻呼机。我的意图是在 ListView 中填充特定表的条目。该方法必须检测数据库中有多少表,并为水平视图分页器设置尽可能多的页面。(一页一表 - 每页仅包含一个 ListView 以显示相应表的条目)。
这是我的尝试。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewp);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);
myPager.setAdapter(adapter);
DbHelper helper = new DbHelper(this);
count = helper.countTables();
myPager.setCurrentItem(count);
temp = helper.countTables() - 1;
temp1 = helper.countTables() - 1;
}
这是页面适配器类。
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return count;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (position == count-1) {
view = inflater.inflate(R.layout.middle, null);
ListView list = (ListView) view.findViewById(R.id.listView1);
helper = new DbHelper(Listing.this);
String tabName = helper.getTableName(temp1);
dataset_cursor = helper.getAll(tabName);
startManagingCursor(dataset_cursor);
adapter = new NoteAdapter(dataset_cursor);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
if (position < temp) {
temp--;
temp1--;
// int hg = temp - position;
// int tempo = temp - hg;
view = inflater.inflate(R.layout.left, null);
ListView list1 = (ListView) view.findViewById(R.id.listView1);
helper = new DbHelper(Listing.this);
String tabName = helper.getTableName(temp1);
dataset_cursor1 = helper.getAll(tabName);
startManagingCursor(dataset_cursor1);
adapter1 = new NoteAdapter(dataset_cursor1);
list1.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
Toast.makeText(getBaseContext(), tabName, Toast.LENGTH_LONG);
temp--;
temp1--;
}
if (position > temp) {
temp++;
temp1++;
// int hg = position - temp;
// int tempo = temp - hg;
view = inflater.inflate(R.layout.right, null);
ListView list2 = (ListView) view.findViewById(R.id.listView1);
helper = new DbHelper(Listing.this);
String tabName = helper.getTableName(temp1);
dataset_cursor2 = helper.getAll(tabName);
startManagingCursor(dataset_cursor2);
adapter2 = new NoteAdapter(dataset_cursor2);
list2.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
}
((ViewPager) collection).addView(view, 0);
return view;
}
我希望最后一个数据库表成为默认页面。当我向左滑动时,它应该转到倒数第二个表格,依此类推。在尝试这个时,我几乎得到了我需要的东西。除了这个问题。
END -- TABLE6 --(向左滑动)-- TABLE5 --(向左滑动) -- TABLE5 --(向左滑动)-- TABLE3 --(向左滑动)-- TABLE2 --(向左滑动)-- TABLE1 - -结束
END -- TABLE1 --(向右滑动)-- TABLE2 --(向右滑动)-- TABLE2 --(向右滑动)-- TABLE3 --(向右滑动)-- TABLE4 -- (向右滑动)-- TABLE5 - -结束
我做对了。这里可能是什么错误。还有其他方法可以实现吗?