Search code examples
androidbuttonback

Doesnt go normal after hitting back button


When I open a certain class which extends ArrayAdapter there is something strange happening when I go to another screen en hit the back button. I set that for certain values the background of the inflated xml file should be set to grey (200,200,200). But when I hit the back button now, everything is grey, altough it is displaying good when I just go there through the main screen (and not hitting the back button...) Class is looking like this:

Code of the override getView method:

@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!


Solution

  • If you are certain that correctAnswersPrev is less than two and correct is not empty, then you need to create a default color for the adapter's view recycler:

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

    This explicitly returns the background color to transparent when it should not be gray.