如何将值从 sqlite 数据库传递到 viewpager?
How can i pass values from sqlite database to viewpager?
这是我的模型。 (有两个问题的卡片)
public class Card implements Serializable {
private String firstQuestion;
private String secondQuestion;
public Card(String firstQuestion, String secondQuestion) {
this.firstQuestion = firstQuestion;
this.secondQuestion = secondQuestion;
}
public String getFirstQuestion() {
return firstQuestion;
}
public String getSecondQuestion() {
return secondQuestion;
}
}
这是我的Activity。适配器将值从 ArrayList 传递到 ViewPager。
public class MainActivity extends FragmentActivity {
private ViewPager vpCardPager;
private PagerAdapter pagerAdapter;
private ArrayList<Card> cardList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cardList = DatabaseManager.cardsList;
vpCardPager = findViewById(R.id.vpCardPager);
FragmentManager fragmentManager = getSupportFragmentManager();
pagerAdapter = new FragmentPagerAdapter(fragmentManager) {
@NonNull
@Override
public Fragment getItem(int position) {
CardFragment cardFragment = CardFragment.newInstance(cardList.get(position));
return cardFragment;
}
@Override
public int getCount() {
return cardList.size();
}
};
vpCardPager.setAdapter(pagerAdapter);
}
}
在这个 class 中,我创建了数据库并尝试将值从游标传递到 MainActivity 中的 ArrayList,但是,当我开始调试时,调试器向我显示 Main[= 中的 ArrayList 22=] 未填充值。我应该怎么做才能解决这个问题??
public class DatabaseManager extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "cards";
private static final int DATABASE_VERSION = 1;
public static final String FIRST_QUESTION = "firstQuestion";
public static final String SECOND_QUESTION = "secondQuestion";
public static final String TABLE_CARD = "Card";
public static final String ID = "_id";
public static ArrayList<Card> cardsList = new ArrayList<>();
SQLiteDatabase cardsDatabase;
public DatabaseManager(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(@NonNull SQLiteDatabase cardsDB) {
onUpdateDataBase(cardsDatabase);
}
@Override
public void onUpgrade(SQLiteDatabase cardsDB, int userDataBaseVersion, int SQLiteHelperDataBaseVersion) {
cardsDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
onUpdateDataBase(cardsDatabase);
}
public void onUpdateDataBase(@NonNull SQLiteDatabase cardsDB) {
cardsDatabase.execSQL("CREATE TABLE " + TABLE_CARD + "("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ FIRST_QUESTION + " TEXT,"
+ SECOND_QUESTION + " TEXT);");
insertCard(cardsDatabase, "one", "two");
insertCard(cardsDatabase, "three", "four");
insertCard(cardsDatabase, "five", "six");
setCardsList();
cardsDatabase.close();
}
public void insertCard(@NonNull SQLiteDatabase cardsDB,
String firstQuestion,
String secondQuestion) {
ContentValues cardsValues = new ContentValues();
cardsValues.put(FIRST_QUESTION, firstQuestion);
cardsValues.put(SECOND_QUESTION, secondQuestion);
cardsDatabase.insert(TABLE_CARD, null, cardsValues);
}
public void setCardsList() {
Cursor cursor = cardsDatabase.query(TABLE_CARD,
new String[]{ID, FIRST_QUESTION, SECOND_QUESTION},
null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (cursor.moveToNext()) {
cardsList.add(new Card(cursor.getString(1), cursor.getString(2)));
}
}
cursor.close();
}
}
以防万一,我附上Fragment代码
public class CardFragment extends Fragment {
public Card currentCard;
private TextView firstQuestion;
private TextView secondQuestion;
private static final String ARG_PARAM1 = "com.example.orgame.card";
public static CardFragment newInstance(Card card) {
CardFragment fragment = new CardFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM1, card);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
currentCard = (Card) getArguments().getSerializable(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_card, container, false);
firstQuestion = rootView.findViewById(R.id.firstQuestion);
secondQuestion = rootView.findViewById(R.id.secondQuestion);
firstQuestion.setText(currentCard.getFirstQuestion());
secondQuestion.setText(currentCard.getSecondQuestion());
return rootView;
}
}
SQLiteOpenHelper
有自己的内部 SQLiteDatabase
。您可以通过一些 get
函数(例如 getWritableDatabase
)从助手获取数据库,使用这些函数而不是您自己的 SQLiteDatabase cardsDatabase;
,您在提供的代码中没有初始化它,这意味着您每当您使用 DatabaseManager
时,可能会得到一个 NPE
,因此 ArrayList
不会填充。
这是我的模型。 (有两个问题的卡片)
public class Card implements Serializable {
private String firstQuestion;
private String secondQuestion;
public Card(String firstQuestion, String secondQuestion) {
this.firstQuestion = firstQuestion;
this.secondQuestion = secondQuestion;
}
public String getFirstQuestion() {
return firstQuestion;
}
public String getSecondQuestion() {
return secondQuestion;
}
}
这是我的Activity。适配器将值从 ArrayList 传递到 ViewPager。
public class MainActivity extends FragmentActivity {
private ViewPager vpCardPager;
private PagerAdapter pagerAdapter;
private ArrayList<Card> cardList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cardList = DatabaseManager.cardsList;
vpCardPager = findViewById(R.id.vpCardPager);
FragmentManager fragmentManager = getSupportFragmentManager();
pagerAdapter = new FragmentPagerAdapter(fragmentManager) {
@NonNull
@Override
public Fragment getItem(int position) {
CardFragment cardFragment = CardFragment.newInstance(cardList.get(position));
return cardFragment;
}
@Override
public int getCount() {
return cardList.size();
}
};
vpCardPager.setAdapter(pagerAdapter);
}
}
在这个 class 中,我创建了数据库并尝试将值从游标传递到 MainActivity 中的 ArrayList,但是,当我开始调试时,调试器向我显示 Main[= 中的 ArrayList 22=] 未填充值。我应该怎么做才能解决这个问题??
public class DatabaseManager extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "cards";
private static final int DATABASE_VERSION = 1;
public static final String FIRST_QUESTION = "firstQuestion";
public static final String SECOND_QUESTION = "secondQuestion";
public static final String TABLE_CARD = "Card";
public static final String ID = "_id";
public static ArrayList<Card> cardsList = new ArrayList<>();
SQLiteDatabase cardsDatabase;
public DatabaseManager(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(@NonNull SQLiteDatabase cardsDB) {
onUpdateDataBase(cardsDatabase);
}
@Override
public void onUpgrade(SQLiteDatabase cardsDB, int userDataBaseVersion, int SQLiteHelperDataBaseVersion) {
cardsDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
onUpdateDataBase(cardsDatabase);
}
public void onUpdateDataBase(@NonNull SQLiteDatabase cardsDB) {
cardsDatabase.execSQL("CREATE TABLE " + TABLE_CARD + "("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ FIRST_QUESTION + " TEXT,"
+ SECOND_QUESTION + " TEXT);");
insertCard(cardsDatabase, "one", "two");
insertCard(cardsDatabase, "three", "four");
insertCard(cardsDatabase, "five", "six");
setCardsList();
cardsDatabase.close();
}
public void insertCard(@NonNull SQLiteDatabase cardsDB,
String firstQuestion,
String secondQuestion) {
ContentValues cardsValues = new ContentValues();
cardsValues.put(FIRST_QUESTION, firstQuestion);
cardsValues.put(SECOND_QUESTION, secondQuestion);
cardsDatabase.insert(TABLE_CARD, null, cardsValues);
}
public void setCardsList() {
Cursor cursor = cardsDatabase.query(TABLE_CARD,
new String[]{ID, FIRST_QUESTION, SECOND_QUESTION},
null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (cursor.moveToNext()) {
cardsList.add(new Card(cursor.getString(1), cursor.getString(2)));
}
}
cursor.close();
}
}
以防万一,我附上Fragment代码
public class CardFragment extends Fragment {
public Card currentCard;
private TextView firstQuestion;
private TextView secondQuestion;
private static final String ARG_PARAM1 = "com.example.orgame.card";
public static CardFragment newInstance(Card card) {
CardFragment fragment = new CardFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM1, card);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
currentCard = (Card) getArguments().getSerializable(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_card, container, false);
firstQuestion = rootView.findViewById(R.id.firstQuestion);
secondQuestion = rootView.findViewById(R.id.secondQuestion);
firstQuestion.setText(currentCard.getFirstQuestion());
secondQuestion.setText(currentCard.getSecondQuestion());
return rootView;
}
}
SQLiteOpenHelper
有自己的内部 SQLiteDatabase
。您可以通过一些 get
函数(例如 getWritableDatabase
)从助手获取数据库,使用这些函数而不是您自己的 SQLiteDatabase cardsDatabase;
,您在提供的代码中没有初始化它,这意味着您每当您使用 DatabaseManager
时,可能会得到一个 NPE
,因此 ArrayList
不会填充。