Question

I have a situation similar to this ViewPager. Getting wrong cursor in SimpleCursorAdapter and this ViewPager Cursor positon but the questions had no applicable answer.

Situation

I am loading the views in my viewpager from a database (cursor). When I set the text for the items (Buttons, TextViews etc) it does this with the correct cursor (in the expected position). But when i try to button.getText() it shows me the text from the next position in the database (same with buttons etc). If the user swipes forward, the cursor returns values in the next position and if the user swipes backward, it returns values in the previous position

So at position x swiping forward returns cursor at position x + 1 and swiping backwards returns cursor at postion x - 1. How can i solve this? This is my PagerAdapter.instantiateItem

    public Object instantiateItem(View view, int position) {

        cursor.moveToPosition(position);
        int cus = cursor.getPosition();
        Log.d(DBHelper.TAG, Integer.toString(cus) + " current cursor position ");

        final ViewPager pager = (ViewPager) view.findViewById(R.id.pager_nav);
        ScrollView layout = (ScrollView) inflater.inflate(R.layout.question_activity, null);

        if (cursorCategory != null && cursorCategory.moveToFirst()){
            String s = cursorCategory.getString(cursorCategory.getColumnIndex("name"));
            ((TextView)layout.findViewById(R.id.text_category)).setText(s);
        }

        Typeface tpf = Typeface.createFromAsset(context.getAssets(), "Roboto-Light.ttf");
        String text = cursor.getString(cursor.getColumnIndex("question_text"));
        TextView question_text =  (TextView)layout.findViewById(R.id.text_question);
        question_text.setText(text);
        Log.d("question_text", question_text.getText() + " " + position + " " + text);
        question_text.setTypeface(tpf);

        final String qPos = Integer.toString(position + 1) + ".";
        TextView question_position = (TextView) layout.findViewById(R.id.text_position);
        question_position.setText(qPos);
        question_position.setTypeface(tpf);

        this.rightAnswer = cursor.getString(cursor.getColumnIndex(DBHelper.RIGHT_ANSWER));
        this.wrongAnswer1 = cursor.getString(cursor.getColumnIndex(DBHelper.WRONG_ANSWER1));
        this.wrongAnswer2 = cursor.getString(cursor.getColumnIndex(DBHelper.WRONG_ANSWER2));

        optionsArray = new ArrayList<String>();
        optionsArray.clear();
        optionsArray.add(this.rightAnswer);
        optionsArray.add(this.wrongAnswer1);
        optionsArray.add(this.wrongAnswer2);
        Collections.shuffle(optionsArray);

        option1 = (Button) layout.findViewById(R.id.button_option1);
        option1.setText((CharSequence) this.optionsArray.get(0));

        option2 = (Button) layout.findViewById(R.id.button_option2);
        option2.setText((CharSequence) this.optionsArray.get(1));

        option3 = (Button) layout.findViewById(R.id.button_option3);
        option3.setText((CharSequence) this.optionsArray.get(2));

        if (this.optionsArray.get(0).equalsIgnoreCase(this.rightAnswer)) {
            this.option1.setTag(DBHelper.RIGHT_ANSWER);
        } else if (!this.rightAnswer.equalsIgnoreCase((String) this.optionsArray.get(0))) {
            this.option1.setTag("wrong");
        }

        if (this.rightAnswer.equalsIgnoreCase((String) this.optionsArray.get(1))) {
            this.option2.setTag(DBHelper.RIGHT_ANSWER);
        } else if (!this.rightAnswer.equalsIgnoreCase((String) this.optionsArray.get(1))) {
            this.option2.setTag("wrong");
        }

        if (this.rightAnswer.equalsIgnoreCase((String) this.optionsArray.get(2))) {
            this.option3.setTag(DBHelper.RIGHT_ANSWER);
        } else if (!this.rightAnswer.equalsIgnoreCase((String) this.optionsArray.get(2))){
            this.option3.setTag("wrong");
        }

        private int getItem(int i) {
            return i += pager.getCurrentItem();
        }
    });

    option1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String Tag = option1.getTag().toString();
            Toast.makeText(context, Tag + " " + option1.getText(), Toast.LENGTH_SHORT).show();

        }
    });

    option2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String Tag = option2.getTag().toString();
            Toast.makeText(context, Tag + " " + option2.getText(), Toast.LENGTH_SHORT).show();

        }
    });

    option3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String Tag = option3.getTag().toString();
            Toast.makeText(context, Tag + " " + option3.getText(), Toast.LENGTH_SHORT).show();

        }
    });
}

And my onLoadFinished()

    @Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    switch (loader.getId()) {
    case QUESTION_TEXT:
        Log.d(" Selection Position", " Selection Position" + selcectionPosition );
        Log.d(" Activity Position", cursor.getPosition() + " Activity Position");
        mAdapter.swapCursor(cursor);
        mAdapter.notifyDataSetChanged();
        pager.setCurrentItem(selcectionPosition, true);
        break;
    }
}

Hoping to get some help.

Was it helpful?

Solution

Disclaimer: It might not be the solution to your problem.

I faced the same problem. So after reading what you wrote, I searched around for something that would help us.

I solved it by using this: How do you get the current page number of a ViewPager for Android?

On my project, I had a button on each page, that on click would trigger a dialog, this dialog would then get a string from the current position of a cursor.

The position on the cursor didn't match the current page being displayed, because, ViewPager changes the cursor's position to get data to preload pages.

So you need to force the the cursor to move to the correct position, the one you're viewing.

To do that, you use cursor.moveToPosition(viewpager.getCurrentItem()). To make sure the cursor follows what you are viewing.

I hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top