0

我是新來的android編程,我需要從sqlite數據庫加載到viewpager每個行(我的SQLite數據庫包含字段「ID,名稱,年齡,地址」),以便我可以能夠導航到每一行左右滑動。我無法找到正確的解決方案。我一直在試圖解決這個問題一個星期。如何將SQLite數據加載到viewpager中?

如果有人能幫助我解決這個問題,那將是一件幸事。非常感謝你!

+1

你到目前爲止嘗試過什麼? –

+0

我能夠使用左右輕掃手勢瀏覽SQLite記錄,並將每個字段加載到文本視圖中。我可以使用cursor.movetonext,cursor.movetoprevious等來加載每條記錄。我已經看到使用viewpager方法的應用程序,該方法在瀏覽頁面時添加了很棒的動畫,但是我甚至沒有接近甚至將SQLite數據加載到視圖頁中我不確定如何。希望您能夠幫助我。 –

+0

我能夠在viewpager中使用一些代碼,並且能夠創建一個viewpager示例,但是其中的很多示例僅顯示了固定數量的頁面。我需要根據我的SQLite數據庫中的數據設置頁面數量和每個頁面的數據。 –

回答

0

經過大量的研究和嘗試,我能夠想出一個工作解決方案。下面的代碼全部列在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; 
    } 
} 

我從數據庫加載表到光標對象,並將其連接到我的CursorPagerAdapter。每行數據的加載在InstantiateItem對象中完成。

相關問題