0

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!

Jack Commonw
  • 395
  • 6
  • 16

1 Answers1

1

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.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks! It does the trick. Isn't this strange? Why do I have to add an else statement? It is suppose to go only through the if its statement is true... – Jack Commonw Oct 01 '12 at 22:03
  • 1
    It's strange until you know what's happening _behind_ the scenes. Watch Android's Romain Guy discuss [this topic](http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html), you'll discover a few quick tips to speed up your code. Glad I could help! – Sam Oct 01 '12 at 22:07