I am Implementing a custom Horizontal View Pager. My Intention is to populate the entries of a particular table in a ListView. The method have to detect how many tables are there in the database and set as many pages for the horizontal view pager. (One Page for One TABLE – Each page contains only one ListView to showing entries of the corresponding TABLE).
Here’s my tries.
@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;
}
here’s the page adapter class.
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;
}
I want the Last Database Table to be the default page. when I swipe left It should go to the second-last Table and so on. While trying this I almost get what I need. Except this problem .
END—TABLE6–(left swipe)–TABLE5–(left swipe)–TABLE5–(left swipe)–TABLE3–(left swipe)–TABLE2–(left swipe)–TABLE1—END
END—TABLE1–(right swipe)–TABLE2–(right swipe)–TABLE2–(right swipe)–TABLE3–(right swipe)–TABLE4–(right swipe)–TABLE5—END
I am doing it correctly. what could be the mistake here. Is there any other way to implement the same ?
The problem is because of your
tempandtemp1variables. Do the following..If you want to maintain multiple dataset cursors then have dataset cursor array declared, which can be easily accessed like
dataset_cursor[position]as shown above.