0

当我打开某个扩展 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;
    }
}

格兹!

4

1 回答 1

1

如果您确定correctAnswersPrev小于两个correct且不为空,则需要为适配器的视图回收器创建默认颜色:

if (correctAnswersPrev < 2 && !correct.equals("") ){
    mView.setBackgroundColor(Color.argb(200, 200, 200, 200));
}
else {
    mView.setBackgroundColor(Color.argb(00, 200, 200, 200));
}

当它不应该是灰色时,这会显式地将背景颜色返回为透明。

于 2012-10-01T21:53:29.737 回答