我有类似这个ViewPager 的情况。在 SimpleCursorAdapter和这个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;
}
}
希望能得到一些帮助。