如何将 SQLite 数据加载到 viewpager 中?
How to load SQLite data into a viewpager?
我是 android 编程新手,我需要将 SQLite 数据库中的每一行加载到 viewpager(我的 SQLite 数据库包含字段 "ID, NAME, AGE, ADDRESS"),以便我能够导航通过向左和向右滑动到每一行。我未能找到正确的解决方案。我已经尝试解决这个问题一个星期了。
如果有人能帮我解决这个问题,那将是一件幸事。非常感谢!
经过大量研究和尝试,我想出了一个可行的解决方案。下面的代码都在 MainActivity 中列出。非常简单。我希望这可以帮助其他人。如果您有任何问题,请发表评论。
public class MainActivity extends AppCompatActivity {
float x1,x2;
float y1, y2;
int currentPage;
ViewPager mViewPager;
String ID;
List<String> listID = new ArrayList<String>();
List<String> listName = new ArrayList<String>();
List<String> listMtrno = new ArrayList<String>();
String SearchString = null;
CursorPagerAdapter mAdapter;
Cursor cursor;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
getSupportActionBar().setDisplayShowHomeEnabled(true);
databaseHelper = new DatabaseHelper(this);
setContentView(R.layout.pager);
databaseHelper = new DatabaseHelper(MainActivity.this);
cursor = databaseHelper.fetch();
mAdapter = new CursorPagerAdapter(this, cursor);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
if (extras != null)
{
SearchString = extras.getString("SearchFlag");
if (SearchString != null) {
mViewPager.setCurrentItem(Integer.parseInt(SearchString) - 1);
} else {
if (cursor.moveToFirst()){
while(!cursor.isAfterLast()){
cursor.moveToNext();
}
}
}
Log.d("MainActivity", "Search(A): " + SearchString);
}
}
private class CursorPagerAdapter extends PagerAdapter {
private Cursor cursor;
private LayoutInflater inflater;
public CursorPagerAdapter(Context context, Cursor cursor) {
Log.d("Main Activity", "MyCursorPagerAdapter.onCreate()");
this.cursor = cursor;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void swapCursor(Cursor cursor) {
//if(cursor != null) {
Log.d("Main Activity", "swapCursor().");
this.cursor = cursor;
//notifyDataSetChanged();
//}
}
@Override
public int getCount() {
if(cursor == null) {
return 0;
} else {
return cursor.getCount();
}
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
cursor.moveToPosition(position);
(view).removeView((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
position = position % cursor.getCount();
cursor.moveToPosition(position);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_listener, null);
TextView f1_recno = layout.findViewById(R.id.TV_recno);
TextView f1_acctno = layout.findViewById(R.id.TV_acctno);
TextView f1_route = layout.findViewById(R.id.TV_route);
TextView fl_name = layout.findViewById(R.id.TV_name);
TextView fl_mtrno = layout.findViewById(R.id.TV_mtrno);
f1_recno.setText((cursor.getPosition() + 1) + " of " + cursor.getCount() + " records");
f1_acctno.setText(cursor.getString(cursor.getColumnIndex("ACCTNO")));
f1_route.setText(cursor.getString(cursor.getColumnIndex("ROUTE")));
fl_name.setText(cursor.getString(cursor.getColumnIndex("NAME")));
fl_mtrno.setText(cursor.getString(cursor.getColumnIndex("MTRNO")));
(view).addView(layout);
return layout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
我将 table 从数据库加载到游标对象,并将其连接到我的 CursorPagerAdapter。每行数据的加载都是在 InstantiateItem 对象中完成的。
我是 android 编程新手,我需要将 SQLite 数据库中的每一行加载到 viewpager(我的 SQLite 数据库包含字段 "ID, NAME, AGE, ADDRESS"),以便我能够导航通过向左和向右滑动到每一行。我未能找到正确的解决方案。我已经尝试解决这个问题一个星期了。
如果有人能帮我解决这个问题,那将是一件幸事。非常感谢!
经过大量研究和尝试,我想出了一个可行的解决方案。下面的代码都在 MainActivity 中列出。非常简单。我希望这可以帮助其他人。如果您有任何问题,请发表评论。
public class MainActivity extends AppCompatActivity {
float x1,x2;
float y1, y2;
int currentPage;
ViewPager mViewPager;
String ID;
List<String> listID = new ArrayList<String>();
List<String> listName = new ArrayList<String>();
List<String> listMtrno = new ArrayList<String>();
String SearchString = null;
CursorPagerAdapter mAdapter;
Cursor cursor;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
getSupportActionBar().setDisplayShowHomeEnabled(true);
databaseHelper = new DatabaseHelper(this);
setContentView(R.layout.pager);
databaseHelper = new DatabaseHelper(MainActivity.this);
cursor = databaseHelper.fetch();
mAdapter = new CursorPagerAdapter(this, cursor);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
if (extras != null)
{
SearchString = extras.getString("SearchFlag");
if (SearchString != null) {
mViewPager.setCurrentItem(Integer.parseInt(SearchString) - 1);
} else {
if (cursor.moveToFirst()){
while(!cursor.isAfterLast()){
cursor.moveToNext();
}
}
}
Log.d("MainActivity", "Search(A): " + SearchString);
}
}
private class CursorPagerAdapter extends PagerAdapter {
private Cursor cursor;
private LayoutInflater inflater;
public CursorPagerAdapter(Context context, Cursor cursor) {
Log.d("Main Activity", "MyCursorPagerAdapter.onCreate()");
this.cursor = cursor;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void swapCursor(Cursor cursor) {
//if(cursor != null) {
Log.d("Main Activity", "swapCursor().");
this.cursor = cursor;
//notifyDataSetChanged();
//}
}
@Override
public int getCount() {
if(cursor == null) {
return 0;
} else {
return cursor.getCount();
}
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
cursor.moveToPosition(position);
(view).removeView((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
position = position % cursor.getCount();
cursor.moveToPosition(position);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_listener, null);
TextView f1_recno = layout.findViewById(R.id.TV_recno);
TextView f1_acctno = layout.findViewById(R.id.TV_acctno);
TextView f1_route = layout.findViewById(R.id.TV_route);
TextView fl_name = layout.findViewById(R.id.TV_name);
TextView fl_mtrno = layout.findViewById(R.id.TV_mtrno);
f1_recno.setText((cursor.getPosition() + 1) + " of " + cursor.getCount() + " records");
f1_acctno.setText(cursor.getString(cursor.getColumnIndex("ACCTNO")));
f1_route.setText(cursor.getString(cursor.getColumnIndex("ROUTE")));
fl_name.setText(cursor.getString(cursor.getColumnIndex("NAME")));
fl_mtrno.setText(cursor.getString(cursor.getColumnIndex("MTRNO")));
(view).addView(layout);
return layout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
我将 table 从数据库加载到游标对象,并将其连接到我的 CursorPagerAdapter。每行数据的加载都是在 InstantiateItem 对象中完成的。