Skip to content
Advertisement

How can i pass values from sqlite database to viewpager?

There is my model. (Card with two questions)

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;
    }
}

This is my Activity. Adapter pass values from ArrayList to the 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);
    }
}

In this class I create DataBase and try to pass values from cursor to ArrayList in MainActivity, but, when I start to debug, the debugger shows me that ArrayList in MainActivity is not filled with values. What I should do to fix this??

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();
    }
}

Just in case, I will attach the Fragment code

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;
    }
}

Advertisement

Answer

SQLiteOpenHelper has its own internal SQLiteDatabase. You can get the database from the helper via a few get functions like getWritableDatabase, use those instead of your own SQLiteDatabase cardsDatabase; which at no point in the provided code are you initializing it, meaning your probably getting a NPE whenever you use the DatabaseManager, thus the ArrayList won’t fill.

Advertisement