当我打开一个扩展ArrayAdapter的某个类时,当我转到另一个屏幕并点击后退按钮时会发生奇怪的事情。我设置为某些值,膨胀的xml文件的背景应设置为灰色(200,200,200)。但是当我现在点击后退按钮时,一切都是灰色的,当我刚刚通过主屏幕(而不是按下后退按钮......)时显示效果很好......类看起来像这样:
覆盖getView方法的代码:
@Override
public View getView(int position, View v, ViewGroup parent) {
View mView = v;
if (mView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(R.layout.levelselector_item, null);
}
TextView level = (TextView) mView.findViewById(R.id.tvLevel);
TextView levelScore = (TextView) mView.findViewById(R.id.tvLevelScore);
if (mView != null) {
level.setText(Integer.toString(getItem(position).getLevel()));
loadDataBase();
int correctAnswers = myDbHelper.getCorrectAnswers(getItem(position).getLevel());
int correctAnswersPrev = 0;
int correctAnswersPrev = myDbHelper.getCorrectAnswersPrev(getItem(position).getLevel());
String correct = Integer.toString(correctAnswers);
levelScore.setText(correct + "/60");
level.setTypeface(font);
levelScore.setTypeface(font);
if (correctAnswersPrev < 2 && !correct.equals("") ){
mView.setBackgroundColor(Color.argb(200, 200, 200, 200));
}
}
return mView;
}
private void loadDataBase() {
// TODO Auto-generated method stub
myDbHelper = new DataBaseHelper(C);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
Grtz!
答案 0 :(得分:1)
如果您确定correctAnswersPrev
小于2且correct
不为空,则需要为适配器的视图回收器创建默认颜色:
if (correctAnswersPrev < 2 && !correct.equals("") ){
mView.setBackgroundColor(Color.argb(200, 200, 200, 200));
}
else {
mView.setBackgroundColor(Color.argb(00, 200, 200, 200));
}
当背景颜色不应为灰色时,它会明确地将背景颜色返回到透明。