我有類似於此ViewPager. Getting wrong cursor in SimpleCursorAdapter和此ViewPager Cursor positon的情況,但問題沒有 適用答案。ViewPager偏移光標位置
形勢
我加載在我viewpager意見從數據庫(光標)。當我爲這些項目設置文本(Buttons
,TextViews
等)時,它使用正確的光標(在預期位置)執行此操作。 但是當我嘗試button.getText()
它讓我從下一個位置在數據庫中的文本(相同的按鈕等)。如果用戶刷前進,將光標放在下一個位置返回值,如果用戶揮筆落後,這在以前的位置
因此,在刷卡前鋒位置x
返回值在x + 1
位置返回光標,然後滑動向後,在現在的位置返回遊標x - 1
。 我該如何解決這個問題?這是我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();
}
});
}
而且我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;
}
}
希望能得到一些幫助。